Sahand
Sahand

Reputation: 8370

How to compile Java project with IntelliJ and run in terminal (NoClassDefFoundError)

I like IntelliJ for it's ease of use and great IDE functionality. However, due to the nature of my project, I want to run the relevant .class files through the terminal after having built everything in IntelliJ. My structure looks like this:

out/
|
|-production/
|  |
|  |-AILab2/
|     |
|     |-TTT/
|     |  |--Main.class
|     |  |--Constants.class
|     |  |--Deadline.class
|     |  |--pipe|
|     |  |--etc...
|     |
|     |-TTT3
|        |--Constants.class
|        |--Deadline.class
|        |--etc...
src
|
|-TTT
|  |--Main.java
|  |--Constants.java
|  |--Deadline.java
|  |--etc...
|
|-TTT3
   |--Constants.java
   |--Deadline.java
   |--etc...

When I go into the terminal to run, I do the following:

~/IdeaProjects/AILab2/out/production/AILab2/TTT$ java Main init verbose < pipe | java Main > pipe
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: TTT/Main (wrong name: Main)
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: TTT/Main (wrong name: Main)

I've searched the web and I don't understand the reason for this error. I've run previous projects the same way, but this time, it doesn't work. Grateful for help.

Edit: Changing terminal command to include TTT resulted in same message:

$ java TTT.Main init verbose < pipe | java TTT.Main > pipe
Error: Could not find or load main class TTT.Main
Caused by: java.lang.ClassNotFoundException: TTT.Main
Error: Could not find or load main class TTT.Main
Caused by: java.lang.ClassNotFoundException: TTT.Main

Upvotes: 1

Views: 157

Answers (1)

GhostCat
GhostCat

Reputation: 140543

The name of the class must include the package (TTT.Main), and you have to invoke Java in the parent directory, the one that contains the TTT folder.

And rest assured: it always works like this. The only explanation for "it worked like this before" is: your classes weren't in a distinct package then!

Beyond that, you have to learn how classes are found within the class path. Start reading here https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html for example.

Upvotes: 1

Related Questions