I keep getting errors when I make my .class
part of a package
and try to run it from cmd.
Here's the code that works after using javac
and then java:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
and then the code that does not work:
package com;
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
giving me this error after trying to run the program via the command: java HelloWorld
:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: com/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Here's what I've tried doing so far:
java -cp . HelloWorld
java -cp . com.HelloWorld
java -cp . com/HelloWorld
java HelloWorld
java com.HelloWorld
java com/HelloWorld
Keep in mind that javac
returns with no errors and that simply removing package com;
solves the problem. Sometimes in other scenarios I get an error that says the main class file cannot be found or something along those lines.
What am I doing wrong?
When you compile the java code, use -d, in your case, it would be
After the above command, java compiler generate a folder named "com", under the "com" folder, you will see your HelloWorld.class
Then under the same folder as you run javac, run the following command
Packages are directly related to the expected directory location of the file.
That is, if you have a source file with the package directive of
com
, it is expected that the file will live in thecom
directory.In your
HelloWorld
example, it would be expected that theHelloWorld.java
file would be stored in thecom
directory, likecom\HelloWorld.java
When you compile the file, it will create a class file called
HelloWorld.class
in thecom
directory, likecom\HelloWorld.class
This way, when Java goes looking for the
com.HelloWorld
class, it would actually be searching it's class path and appendingcom\HelloWorld.class
to it until it finds your class file or runs out of class pathExample
So, I copied your
HelloWorld.java
(with package) example toC:\java\com\HelloWord.java
From the command line, I changed to the
C:\java
directory...Then I compiled the
HelloWorld.java
Then I ran it...
You might like to have a read through Packages tutorial
You should compile it first by typing this command in CMD, for exemple your file is in c:\ directory :
After that you can run the result by typing:
The syntax is:
So you may try:
java com.HelloWorld
which would expectcom/HelloWorld.class
file to be present as classpath by default points to the current directory (if not specified).In case you're in different folder, try specifying classpath:
For further explanation, check: How do I run Java .class files?
Suppose the file locates at C:/projects/com/HelloWorld and you can try the following ways.
1.
java -cp c:/projects com.HelloWorld
2.
cd c:/projects
java com.HelloWorld
(The example 2 is not working in many situation,such as java process.)
if there is no package declaration and there will be a little change.
1.
java -cp c:/projects/com HelloWorld
2.
cd c:/projects/com
java HelloWorld
(It's not good with the same reason.)
alternatively,relative path will be ok but has some risk. Last remember put the class file in end of cmd.