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
mvn clean packageto compile the project. The compiled classes will be located in the classes directory under the target directory.-cp- so thatjavaccan locate the.classfiles corresponding to theimportstatements in the code in your question.edu.ntnu.idi.idat.*classes are provided in source form, you might need to designate-sourcepath src/main/javainstead of or in addition to a classpath.javacis not a good strategy for anything other than the simplest cases. Since you have a Maven project, you wantmvn clean compile exec:javaCLASSPATHenvironment variable (if no-cpor similar option is used) - short:cd src/main/javaand thenjavac edu/ntnu/idi/idat/TestCode.java(since the default CLASSPATH contains.) -- a bit better, use-cp .../src/main/javahere...as needed to get the absolute path (obviously, use Maven if already having it setup) -- similar for starting (java edu.ntnu.idi.idat.TestCodehaving the parent directory ofeduin the CLASSPATH or being the current one)