This question already has an answer here:
I'm currently learning Java and the use of my command prompt as a compiler. But every time I execute the java command followed by my test class "Hello" I get the below error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: hello/Hello)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Now I've checked my "CLASSPATH" environment variable and it is correct as follows: .;C:\Program Files\Java\jdk1.7.0_25\bin; I've even tried removing the .; from the beginning of the CLASSPATH but it didn't do anything different. Now my javac command works just fine by creating a .class version of my .java class. But I just can't get it to actually execute the java command.
The name of my class is Hello so I typed javac Hello.java to compile my file as a class file and it worked. But when I enter: java Hello is when I get the above error messages. I've tested the program on my NetBeans IDE that I made it in, and it works perfectly fine with no errors.
What could possibly be going on that would prevent me from executing my java command to run a .class file?
The name of my class is Hello so I typed javac Hello.java to compile my file as a class file and it worked. But when I enter: java Hello
The most likely problem is that while running your java program you are not putting the complete class name along with the package structure. It should be run as mentioned here:
java packagenhierarchy.Hello
Assuming your package name is com.my.hello and your main class name is Hello then it should be run from the directory containing the top level package as:
java com.my.hello.Hello
UPDATE: As per your comments and knowing the working directory, here is what you should run:
java -cp C:\hello\src\hello hello.Hello
You need to understand how java tool works which is little different than how javac works. In order to run a program with java command-line command:
- The class that has the
main(String[] args)method should be in the classpath. - Instead of type
java Helloyou should use the fully qualified name, such as:
java com.mypackage.Hello
assuming that you set the classpath with the variable CLASSPATH. Otherwise, it should be like this:
java -cp C:\projects\myprojct\bin com.mypackage.Hello
assuming that bin is the root directory that has the following hierarchy:
bin -
|
com -
|
mypackage -
|
Hello.class
Note that if you don't use neither CLASSPATH nor -cp nor -classpath, then the current directory is by default is in the classpath. In other words, the following should work:
cd C:\projects\myprojct\bin
java com.mypackage.Hello
Maybe your current directory is not in the Class path. Try
java -cp pathToYourHelloCompiledFile Hello
You must have a Hello.class file in your above folder whose path you provide as the class path.
来源:https://stackoverflow.com/questions/18094232/cmd-java-not-working