Reputation: 2154
The file FactoryDemo.java
is compiled successfully and in it there is the
package home.city.Desktop.factorydemo;
I compile it by using javac -d / FactoryDemo.java
and nothing wrong is reported.
But when I try to run it by using java home.city.Desktop.factorydemo.FactoryDemo
, it reports:
Exception in thread "main" java.lang.NoClassDefFoundError: home/city/Desktop/factorydemo/FactoryDemo
Caused by: java.lang.ClassNotFoundException: home.city.Desktop.factorydemo.FactoryDemo
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: home.city.Desktop.factorydemo.FactoryDemo. Program will exit.
I have tried many times but I could not find the solution to the problem. Thanks for your help in advance.
Upvotes: 1
Views: 135
Reputation: 10147
FactoryDemo Class is created under root directory, you should type following command ( . instead of /)
javac -d . FactoryDemo.java
It creates home.city.Desktop.factorydemo
directories starting from current directory.
If you want class created under root directory. You must type following command to execute.
java -classpath / home.city.Desktop.factorydemo.FactoryDemo
Upvotes: 1
Reputation: 21912
Your compiled class must be in a directory called:
home/city/Desktop/factorydemo
On Unix, just run this:
mkdir -p home/city/Desktop/factorydemo; mv FactoryDemo.class home/city/Desktop/factorydemo/
For best practice, simply move your Java source file in that folder too so the compiler outputs the file to the right spot.
Upvotes: 1