找不到主类HelloWorld [英] Could not find main class HelloWorld
问题描述
我在以下文件夹中安装了Java 1.7.0 C:\Program Files \ Java
。我的操作系统是带有Service Pack 3的Windows XP(版本2002)。
I installed Java 1.7.0 in the following folder C:\Program Files\Java
. My operating system is Windows XP(Version 2002) with Service pack 3.
我设置的环境变量是:
-
CLASSPATH :
C:\Program Files\Java \ jdk1.7.0 \ jre \ lib \ rt.jar;
路径: C:\Program Files \ Java \ jdk1.7.0 \ bin;
JAVA_HOME : C:\Program Files\Java;
我在这里介绍过我系统中的类名。
I have presented here the class names which are in my system.
接下来我写了一个程序,HelloWorld.java:
Next I wrote a program, HelloWorld.java:
import java.io.*;
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
当我使用编译时javac HelloWorld.java
编译正常。
但是在我发出 java HelloWorld
之后我遇到以下错误:
But after I issue java HelloWorld
I am encountering the below error:
Error: Could not find main class HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:198)
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:299)
at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
at java.lang.ClassLoader.loadClass(ClassLoader.java:422)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
at java.lang.ClassLoader.loadClass(ClassLoader.java:355)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:195)
经过一番搜索,我发现环境变量可能有问题。我试着玩这个,但没有运气。
After a bit of searching around, I found that may be something wrong in the environment variable. I tried to play with that but no luck.
我甚至重新启动了机器,然后我再次尝试运行,但命运相同。
I even RESTARTED the machine and then again I tried to run but with same fate.
推荐答案
您没有设置包含已编译类的类路径! java
如果你不告诉它在哪里找不到任何类。
You are not setting a classpath that includes your compiled class! java
can't find any classes if you don't tell it where to look.
java -cp [compiler outpur dir] HelloWorld
顺便说一下你不需要设置CLASSPATH你已经完成的方式。
Incidentally you do not need to set CLASSPATH the way you have done.
这篇关于找不到主类HelloWorld的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!