I'm trying to run a very simple one class "Hello World" program from the command line in Windows.
The .java
file is located at "C:\Users\UserName\Desktop\direcName". The package is deem
and the class name is test
.
I can cd to the directory and compile it from there using javac test.java
and that works perfectly fine. However, when I try to run it using:
java test
or java -classpath directory test
or java -cp . test
it throws "Exception in thread main
java.lang.NoClassDefFoundError: test (wrong name: deem/test)
.
If I use java deem.test
it says: Error, could not find or load main class deem.main
How can I fix the exception and get my program to run?
Thanks
If the class is located in a package "deem", then you need to include the package name like this.
That should work.
This is a variation of the "common beginners error" with running Java programs from the command line.
The JVM is in effect telling you that found "test.class" on the search path, but when it read class file, it concluded that the file should have been at "./deem/test.class" or "directory/deem/test.class" ... depending on which "-cp" / "-classpath" argument you actually used
This is now telling you that it cannot find "deem/main.class".
Note that you have now told it to look for a class called "deem.main" instead of "test" or "deem.test". (Or perhaps you just transcribed something incorrectly there.)
The rules are really rather simple:
java
as the first argument after the options. (Not the simple class name. Not the ".class" file name. Not the name of the entry point method.)java
command canfoo.bar.baz.MyClass
maps tofoo/bar/baz/MyClass.class
) ....
is the classpath, then./foo/bar/baz/MyClass.class
.public static void main(String[])
entry point method.So IF ...
deem.test
; i.e. thetest
class is in the packagedeem
, AND./deem/test.class
, ANDTHEN
java -cp . deem.test
should work.