3

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: A picture of the folders and files inside project/src/main

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
5
  • 6
    Your project contains a pom.xml file, indicating it's a Maven project. In the directory containing the pom.xml file, open a Terminal and use the command mvn clean package to compile the project. The compiled classes will be located in the classes directory under the target directory. Commented Nov 16 at 16:16
  • 6
    If you really want to use javac then you need to add options - probably -cp - so that javac can locate the .class files corresponding to the import statements in the code in your question. Commented Nov 16 at 16:26
  • 4
    Or since it looks like those edu.ntnu.idi.idat.* classes are provided in source form, you might need to designate -sourcepath src/main/java instead of or in addition to a classpath. Commented Nov 16 at 16:48
  • 3
    Using pure javac is not a good strategy for anything other than the simplest cases. Since you have a Maven project, you want mvn clean compile exec:java Commented Nov 16 at 18:37
  • directory hierarchy must match package hierarchy, and classes/packages are searched starting in the directories given in the CLASSPATH environment variable (if no -cp or similar option is used) - short: cd src/main/java and then javac edu/ntnu/idi/idat/TestCode.java (since the default CLASSPATH contains .) -- a bit better, use -cp .../src/main/java here ... as needed to get the absolute path (obviously, use Maven if already having it setup) -- similar for starting (java edu.ntnu.idi.idat.TestCode having the parent directory of edu in the CLASSPATH or being the current one) Commented Nov 16 at 21:48

1 Answer 1

3
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

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, that's likely to fail as soon as you start to get external dependencies. The only way to be sure is to derive the actual classpath with dependency:build-classpath

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.