Java “你好,世界!” - 无法找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23285290/
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
"Hello, world!" - Could not find or load main class
提问by yogeshkmrsoni
Computer OS: Windows 7 Language: Java
计算机操作系统:Windows 7 语言:Java
After long time back I am using java, I am getting problem while running the hello world program:
很长一段时间后,我使用 java,在运行 hello world 程序时遇到问题:
public class Hello
{
public static void main (String args[])
{
System.out.println ("Hello World!");
}
}
I saved this code in Hello.java File, and then compiled using cmd:
我把这段代码保存在Hello.java文件中,然后用cmd编译:
C:\Users\XYZ\Desktop>javac -version
javac 1.6.0
C:\Users\XYZ\Desktop>javac Hello.java
C:\Users\XYZ\Desktop>java Hello
Error: Could not find or load main class Hello
C:\Users\XYZ\Desktop>
Can you explain why the error message above was thrown?
你能解释一下为什么会抛出上面的错误信息吗?
采纳答案by Neel
You are missing one "}" at the end of the program
你在程序末尾少了一个“}”
public class Hello
{
public static void main (String args[])
{
System.out.println ("Hello World!");
}
}
and then try
然后尝试
java -cp . Hello
回答by DaveH
try
尝试
java -cp . Hello
I think you do not have the current directory on your classpath
我认为您的类路径上没有当前目录
回答by Baby
try this one:
试试这个:
java -cp . Hello
回答by krisliu
try to remove
尝试删除
package package.path;
wish this will help you.
希望这会帮助你。
回答by Mikel
I experienced exactly the same error and I've got around it changing the directory where I had both my HelloWorld.java file and the HelloWorld.class file created with javac.
我遇到了完全相同的错误,我已经解决了它更改了我的 HelloWorld.java 文件和使用 javac 创建的 HelloWorld.class 文件的目录。
I had first tried all different ways to force the classpath with no success.
我首先尝试了所有不同的方法来强制使用类路径,但没有成功。
The only difference I appreciate between the two directories is that the first one's path had directories containing spaces in their directory names. I need to investigate further.
我欣赏这两个目录之间的唯一区别是第一个路径的目录在其目录名称中包含空格。我需要进一步调查。
Not sure why my javac works in both directories but java doesn't.
不知道为什么我的 javac 可以在两个目录中运行,而 java 不能。
EDIT: I've now tried again in the first directory and it has worked just fine. No idea why it wasn't working before and now unable to repeat the error.
编辑:我现在在第一个目录中再次尝试,它工作得很好。不知道为什么它以前不起作用,现在无法重复错误。
回答by johan indra Permana
I had the same problem as you did. The way I resolved it was by removing the package declaration.
我和你有同样的问题。我解决它的方法是删除包声明。
回答by Astro
remove the package declaration and rerun
删除包声明并重新运行
javac myprogram.java
javac myprogram.java
回答by Abhishek D K
this might help someone down the line who has latest version like java 1.8.0_201
这可能会帮助那些拥有最新版本(如 java 1.8.0_201)的人
set the classpath properly in environment variables.
在环境变量中正确设置类路径。
JAVA_HOME= C:\Program Files\Java\jdk1.8.0_201
CLASSPATH= %JAVA_HOME%\lib.;
JAVA_HOME= C:\Program Files\Java\jdk1.8.0_201
CLASSPATH= %JAVA_HOME%\lib.;
append this JAVA_HOMEin PATHenvironment variable
将此JAVA_HOME附加到PATH环境变量中
note:
remeber to put "."after libfolder to load all the Classes in CLASSPATHvar.
注意:
记得放“.” 在lib文件夹之后加载CLASSPATHvar 中的所有类。
回答by Yiting Zhang
I had the same problem as you did. I use eclipse to write the JAVA code. I resolve the problem by choosing the default choice of Project layout "Create separate folders for sources and class files" when I create a new JAVA project. The problem will occur when I choose the "Use project folder as root for sources and class files" image of Project layout
我和你有同样的问题。我使用eclipse编写JAVA代码。当我创建一个新的 JAVA 项目时,我通过选择项目布局的默认选项“为源文件和类文件创建单独的文件夹”来解决这个问题。当我选择项目布局的“使用项目文件夹作为源和类文件的根目录”图像时会出现问题