Java 找不到主类 HelloWorld
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3005433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find main class HelloWorld
提问by Newbie
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.
我在以下文件夹中安装了 Java 1.7.0 C:\Program Files\Java
。我的操作系统是带有 Service Pack 3 的 Windows XP(2002 版)。
The environment variables which I set are:
我设置的环境变量是:
CLASSPATH:
C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jar;
Path :
C:\Program Files\Java\jdk1.7.0\bin;
JAVA_HOME:
C:\Program Files\Java;
类路径:
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.
我在这里展示了我系统中的类名。
Next I wrote a program, HelloWorld.java:
接下来我写了一个程序,HelloWorld.java:
import java.io.*;
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
When I am compiling using javac HelloWorld.java
it is compiling fine.
当我使用javac HelloWorld.java
它进行编译时,它编译得很好。
But after I issue java HelloWorld
I am encountering the below error:
但是在我发出后,我java HelloWorld
遇到了以下错误:
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.run(URLClassLoader.java:299)
at java.net.URLClassLoader.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.
我什至重新启动了机器,然后我再次尝试运行,但命运相同。
采纳答案by Sean Owen
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
如果你不告诉它在哪里看,就找不到任何课程。
java -cp [compiler outpur dir] HelloWorld
Incidentally you do not need to set CLASSPATH the way you have done.
顺便说一句,您不需要像以前那样设置 CLASSPATH。
回答by unbeli
Tell it where to look for you class: it's in ".", which is the current directory:
告诉它在哪里寻找你的类:它在“.”中,这是当前目录:
java -classpath . HelloWorld
No need to set JAVA_HOME
or CLASSPATH
in this case
无需设置JAVA_HOME
或CLASSPATH
在这种情况下
回答by Andreas Dolk
JAVA_HOME
is not necessary if you start java and javac from the command line. But JAVA_HOME
should point to the real jdk directory, C:\Program Files\Java\jdk1.7.0
in your case.
JAVA_HOME
如果从命令行启动 java 和 javac,则不需要。但在你的情况下JAVA_HOME
,应该指向真正的 jdk 目录C:\Program Files\Java\jdk1.7.0
。
I'd never use the CLASSPATH
environment variable outside of build scripts, especially not global defined. The -cp
flag is better. But in your case, as you do not need additional libraries (rt.jar
doesn't count), you won't need a classpath declaration. A missing -cp
is equivalent to a -cp .
and that's what you need here)
我永远不会在CLASSPATH
构建脚本之外使用环境变量,尤其是没有全局定义的环境变量。该-cp
标志是更好的。但是在您的情况下,由于您不需要额外的库(rt.jar
不算数),因此您不需要类路径声明。一个缺失-cp
相当于一个-cp .
,这就是你在这里需要的)
The (I was pretty sure, that a source file needs one public class... or was it one public class at most?)HelloWorld
class needs to be declared as public
. This actually may be the cause for your problems.
本(我很确定,源文件需要一个公共类......或者最多是一个公共类?)HelloWorld
类需要声明为public
。这实际上可能是导致您出现问题的原因。
回答by Jorn
You either want to add "." to your CLASSPATH to specify the current directory, or add it manually at run time the way unbeli suggested.
您要么想添加“。” 到您的 CLASSPATH 以指定当前目录,或者按照 unbeli 建议的方式在运行时手动添加它。
回答by user85421
Java is not finding where your compiled class file (HelloWorld.class) is. It uses the directories and JAR-files in the CLASSPATH
environment variable for searching if no -cp
or -classpath
option is given when running java.exe.
Java 没有找到您编译的类文件 (HelloWorld.class) 的位置。它使用CLASSPATH
环境变量中的目录和 JAR 文件来搜索运行 java.exe 时是否给出了-cp
或-classpath
选项。
You don't need the rt.jar in the CLASSPATH
, these was only needed for older versions of Java. You can leave it undefined and the current working directory will be used, or just add .
(a single point), separated by ';', to the CLASSPATH
variable to indicate the current directory:
您不需要 rt.jar 中的 rt.jar,CLASSPATH
只有旧版本的 Java 才需要这些。您可以不定义它并使用当前工作目录,或者只是添加.
(一个点),以“;”分隔,CLASSPATH
以指示当前目录的变量:
CLASSPATH:.;C:\...\some.jar
类路径:.;C:\...\some.jar
Alternatively you can use the -cp
or -classpath
option:
或者,您可以使用-cp
或-classpath
选项:
java -cp . HelloWorld
And, as Andreas wrote, JAVA_HOME
is not needed by Java, just for some third-party tools like ant (but should point to the correct location).
而且,正如 Andreas 所写的,JAVA_HOME
Java 不需要,只需要一些第三方工具,如 ant(但应该指向正确的位置)。
回答by BluesPower
Just remove your "classpath" from you environment variable. Then try running:
只需从您的环境变量中删除您的“类路径”。然后尝试运行:
java HelloWorld
This should work fine.
这应该可以正常工作。
回答by d9daniel
I had the same problem. Perhaps, the problem is that you have compiled and executed the class with different Java versions.
我有同样的问题。也许,问题在于您使用不同的 Java 版本编译并执行了该类。
Make sure the version of the compiler is the same as the command "java":
确保编译器的版本与命令“java”相同:
javac -version
java -version
In Linux, use
在 Linux 中,使用
sudo update-alternatives --config java
须藤更新替代品--config java
to change the version of Java.
更改Java的版本。
回答by user3329068
put .; at classpath value in beginning..it will start working...it happens because it searches the class file in classpath which is mentioned in path variable.
放 。; 在开始时的类路径值......它将开始工作......它发生是因为它在路径变量中提到的类路径中搜索类文件。
回答by Anoop Reddy
I have also faced same problem....
我也遇到了同样的问题......
Actually this problem is raised due to the fact that your program .class
files are not saved in that directory. Remove your CLASSPATH from your environment variable (you do no need to set classpath for simple Java programs) and reopen cmd prompt, then compile and execute.
实际上,由于您的程序.class
文件未保存在该目录中,因此引发了此问题。从您的环境变量中删除您的 CLASSPATH(您不需要为简单的 Java 程序设置类路径)并重新打开 cmd 提示符,然后编译并执行。
If you observe carefully your .class
file will save in the same location. (I am not an expert, I am also basic programer if there is any mistake in my sentences please ignore it :-))
如果您仔细观察,您的.class
文件将保存在同一位置。(我不是专家,我也是初级程序员,如果我的句子有任何错误,请忽略它:-))
回答by Chandan Prakash Sharma
It looks that you had done all setup properly but there might be one area where it might be causing problem
看起来您已正确完成所有设置,但可能有一个区域可能导致问题
Check the value of your "CLASSPATH" variable and make sure at the end you kept ;.
检查“CLASSPATH”变量的值,并确保最后保留了;。
Note: ;is for end separator .is for including existing path at the end
注意: ; 用于结束分隔符 。用于在末尾包含现有路径