0

我目前正在学习 Java,并将我的命令提示符用作编译器。但是每次我执行java命令后跟我的测试类“Hello”时,我都会收到以下错误消息:

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)

现在我检查了我的“CLASSPATH”环境变量,它是正确的,如下所示:.;C:\Program Files\Java\jdk1.7.0_25\bin; 我什至尝试删除.; 从 CLASSPATH 开始,但它没有做任何不同的事情。现在,通过创建 .java 类的 .class 版本,我的javac命令可以正常工作。但我就是无法让它实际执行java命令。

我的类的名称是Hello,所以我输入了javac Hello.java来将我的文件编译为类文件,它可以工作。但是当我输入:java Hello时,我得到了上述错误信息。我已经在我制作的 NetBeans IDE 上测试了这个程序,它运行良好,没有错误。

可能发生什么会阻止我执行我的 java 命令来运行 .class 文件?

4

3 回答 3

5

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
于 2013-08-07T03:41:44.973 回答
2

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:

  1. The class that has the main(String[] args) method should be in the classpath.
  2. Instead of type java Hello you 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
于 2013-08-07T03:45:13.133 回答
1

也许您的当前目录不在类路径中。尝试

java -cp pathToYourHelloCompiledFile Hello

您的上述文件夹中必须有一个 Hello.class 文件,您提供其路径作为类路径。

于 2013-08-07T03:46:39.143 回答