java – NoClassDefFoundError but class is there

Advertisements

i have developed a Java project with intelliJ, now i want to run my program via the command line. So i go to the project/out/production/project folder, where my *.class files are located and try to run my file "java FileCopy [args]" (FileCopy.class is there and class inside has the same name) but i get the error "Fehler: Main class FileCopy could not be found or loaded
Ursache: java.lang.NoClassDefFoundError: BelegFileTransfer/FileCopy (wrong name: FileCopy)"

If i want to run my project with intelliJ its no problem.

What am i doing wrong?

My directory structure:

FileTransfer
-> .idea
-> out
--> production
----> FileTransfer (thats where the .class files located and i am running the cmd)
-> src
--> FileTransfer (thats where the .java files located)

Error:
[1]: https://i.stack.imgur.com/Ft2q0.png

>Solution :

It looks like the issue is related to the classpath. When you run the program from the command line, the Java runtime is unable to find the main class "FileCopy" because it is not in the classpath.

To fix this, you can either:

  1. Specify the full package name of the class when running the program: "java FileTransfer.FileCopy [args]"
  2. Add the "out/production" directory to the classpath when running the program: "java -cp out/production FileCopy [args]"

Another solution is that you can create a jar file of your project, then you can run the jar file by running the command "java -jar <name_of_jar>.jar"

You can create a jar file by going to File -> Project Structure -> Artifacts -> + -> JAR -> From modules with dependencies -> select main class -> OK -> Build -> Build Artifacts
Then you can find the jar in the project/out/artifacts/<name_of_jar>.jar

Leave a Reply Cancel reply