Java 错误:无法在 Windows 7 中找到或加载主类 hello
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18801246/
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
Error: Could not find or load main class hello in Windows 7
提问by anil
i am a new java student i am running a hello world program in command prompt but i am getting a error
我是一名新的 Java 学生,我正在命令提示符下运行一个 hello world 程序,但出现错误
class hello{
public static void main(String agrs[]){
system.out.println("Hello world");
}
}
this is my hello world program
这是我的 hello world 程序
G:\java>javac hello.java
G:\java>dir
Volume in drive G has no label.
Volume Serial Number is 32DF-BA6B
Directory of G:\java
14-Sep-13 04:36 PM <DIR> .
14-Sep-13 04:36 PM <DIR> ..
14-Sep-13 04:36 PM 415 hello.class
14-Sep-13 04:35 PM 100 hello.java
2 File(s) 515 bytes
2 Dir(s) 55,645,966,336 bytes free
G:\java>java hello
Error: Could not find or load main class hello
my java path is right
我的java路径是对的
G:\java>path
PATH=G:\Windows\system32;G:\Windows;G:\Windows\System32\Wbem;G:\Windows\System32
\WindowsPowerShell\v1.0\;G:\Program Files\Java\jdk1.7.0_25\bin
but when is use this command then program run .
但是什么时候使用这个命令然后程序运行。
G:\java>java -classpath . hello
Hello world
i want to ask that why is my program not run normally is any problem in my path setting variable ? i want to run my program normally as
我想问一下为什么我的程序不能正常运行是我的路径设置变量有问题吗?我想正常运行我的程序
G:\java>java hello
回答by Pratik Shelar
You need to set the classpath variable as well. Currently you have just set your PATH variable
您还需要设置类路径变量。目前您刚刚设置了 PATH 变量
1)Main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand CLASSPATH environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in .class file.
1)PATH和CLASSPATH的主要区别在于PATH是一个环境变量,用于定位JDK二进制文件,如用于运行java程序和编译java源文件的“java”或“javac”命令。另一方面,系统或应用程序类加载器使用 CLASSPATH 环境变量来定位和加载存储在 .class 文件中的编译 Java 字节码。
2) In order to set PATH in Java you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directory where you have put either your .class file or JAR file which is required by your Java application.
2) 为了在 Java 中设置 PATH,您需要在 PATH 环境变量中包含 JDK_HOME/bin 目录,而为了在 Java 中设置 CLASSPATH,您需要包含所有放置 .class 文件或 JAR 文件的目录您的 Java 应用程序需要。
3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.
3) PATH 和 CLASSPATH 之间的另一个显着区别是 PATH 不能被任何 Java 设置覆盖,但可以通过向“java”和“javac”命令提供命令行选项 -classpath 或 -cp 或使用 Class- 来覆盖 CLASSPATH JAR 存档内的清单文件中的路径属性。
回答by jokochan
set on the system variable from :
在系统变量上设置:
C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_45\bin
to
到
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_45\bin
回答by adairjun
sudo vim /etc/profile
sudo vim /etc/profile
Then you add:
export CLASSPATH=$CLASSPATH:.
然后你添加:
export CLASSPATH=$CLASSPATH:.
Quit vim, then
退出vim,然后
source /etc/profile
source /etc/profile