Reputation: 331
I've seen questions like this on here, but I can't find one that is specific to my situation, so sorry if this is a worn out question.
I've got a class P that is part of a package myname.utils located in ~/JavaClasses/myname/utils. I've got another class Printing that is not declared as a part of any package but is located in ~/JavaClasses/myname/practice. Class Printing imports class P. Both of these classes compile fine from ~/JavaClasses directory. However when I attempt to run the compiled class Printing like this
~/JavaClasses$ java myname/practice/Printing
I get the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: myname/practice/Printing (wrong name: Printing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Upvotes: 1
Views: 3875
Reputation: 2224
The error is not related to myname.utils.P class but it happens because Printing class has no package definition. This means that the real full name of the class is Printing, but if you launch the java command from ~/JavaClasses folder, you are exactly stating that the full name of Printing class is myname.practice.Printing.
You have 2 options to fix this problem:
1) Declare the myname.practice package inside Printing class (I suggest this one)
2) Add the myname.practice folder to the classpath, in this way:
~/JavaClasses$ java -cp myname.practice:. Printing
The .
represents the current directory, and it is necessary so you can still reference the myname.utils.P class from ~/JavaClasses folder.
Side note. At compile time you have no errors because probably you don't specify a classpath when you launch javac and so the current folder it is used. This way, myname.utils.P can be referenced without problem by every class, even by Printing.
Upvotes: 1
Reputation: 3066
This is because you are not using the package name. Yes when java gives the error and the package name for class not found, it uses /
. However those should be replaced with .
. since parts of the package name are always separated with a period and never with a slash. So if I had a package foo.bee
. Then when I tried to run
java foo.bee
It would run currently and all, however if I tried
java foo/bee
it would complain about a class not def error.
Upvotes: 0