I'm curious to know why I get this error when compiling my java file from the terminal. I expect the program to compile into an executable file, yet instead I get errors about my packages not existing. It compiles and runs fine in an IDE.
Here's some example code:
package edu.ntnu.idi.idat;
import edu.ntnu.idi.idat.database.Database;
import edu.ntnu.idi.idat.encryption.Encryption;
import edu.ntnu.idi.idat.utility.Clipboard;
import edu.ntnu.idi.idat.utility.IntegerChecker;
import edu.ntnu.idi.idat.utility.Terminal;
public final class TestCode {
private TestCode() { }
/**
* Test code.
*/
public static void main(
final String[] args
) {
System.out.println("Hello world");
}
}
Here's the command used and the error message:
$ javac src/main/java/edu/ntnu/idi/idat/TestCode.java
src/main/java/edu/ntnu/idi/idat/TestCode.java:2: error: package edu.ntnu.idi.idat.database does not exist
import edu.ntnu.idi.idat.database.Database;
^
src/main/java/edu/ntnu/idi/idat/TestCode.java:3: error: package edu.ntnu.idi.idat.encryption does not exist
import edu.ntnu.idi.idat.encryption.Encryption;
^
src/main/java/edu/ntnu/idi/idat/TestCode.java:4: error: package edu.ntnu.idi.idat.utility does not exist
import edu.ntnu.idi.idat.utility.Clipboard;
^
src/main/java/edu/ntnu/idi/idat/TestCode.java:5: error: package edu.ntnu.idi.idat.utility does not exist
import edu.ntnu.idi.idat.utility.IntegerChecker;
^
src/main/java/edu/ntnu/idi/idat/TestCode.java:6: error: package edu.ntnu.idi.idat.utility does not exist
import edu.ntnu.idi.idat.utility.Terminal;
^
The packages are just classes i made and split into different files.
I compile the code in the terminal from the same directory as in the IDE, so I assume there wouldn't be differences in project layout. Could it be that I need to add some statements in the javac command to compile all the files in src at the same time? From my understanding, that shouldn't really be an issue, but I might be wrong.
Here's a picture of relevant parts of the project structure:

Similarly, I get another error when trying to run the IDE-compiled .class files:
$ java edu.ntnu.idi.idat.TestCode
Error: Could not find or load main class edu.ntnu.idi.idat.TestCode
Caused by: java.lang.ClassNotFoundException: edu.ntnu.idi.idat.TestCode
error: package edu.ntnu.idi.idat.database does not exist
means
your TestCode depends on other packages :
database
encryption
utility
javac src/main/java/edu/ntnu/idi/idat/TestCode.java
with this you are compiling only 1 file so java does not search for other directories. so you got the package does not exist error.
In case of IDE,IDEs will automatically compiles the whole project and set the classpath that is why you are not getting this error in IDE
If you want to run through terminal you can build using maven then run .
when you run
$ java edu.ntnu.idi.idat.TestCode
java wants know where the compiled file is .so give the path based on the class file. If you using maven build use like this
java -cp target/classes edu.ntnu.idi.idat.TestCode
-cp means the classpath. it tells where the class files located