I have a Java program called Main.java, it is located in the following directory:
/home/user/program/Main.java
When I try to run Main.java from the ‘program’ directory, everything goes ok, I use this line:
/home/user/program$ java Main
But when I try to run Main.java from the home directory :
/home$ java /home/user/program/Main
I get :
Exception in thread "main" java.lang.NoClassDefFoundError: /home/user/program/Main
Caused by: java.lang.ClassNotFoundException: .home.user.program.Main
What is the cause of this error?
This is due to your classpath, which will default to the current directory. When you run
java Mainfrom/home/user/programit finds the class in the current directory (since the package seems to be unset, meaning it is the default). Hence, it finds the class in/home/user/program/Main.class.Running
java /home/user/program/Mainfrom/hometries to find the class in the classpath (the current directory) which will look in/home/home/user/programexpecting to find the fileMain.classcontaining a definition of theMainclass with package.home.user.program.You can fix it by specifying your classpath with
-cpor-classpath: