I have Windows 7, installed jdk1.7.0 and its supporting jre7.
My problem is compilation part works perfectly, but while running the Java program I get this error saying:
我有Windows 7,安装了jdk1.7.0和它的支持jre7。我的问题是编译部分工作得很好,但是在运行Java程序时,我遇到了这个错误:
"Could not find or load main class"
“找不到或装入主类”
I am storing all my programs in javalab folder. I have set the path to it. Procedure looks like this:
我将所有程序存储在javalab文件夹中。我已经确定了它的路径。过程是这样的:
C:\Users\user>cd\ C:\>cd javalab C:\javalab>autoexec.bat C:\javalab>set path=C:\Program Files\Java\jdk1.7.0\bin C:\javalab>javac p1.java C:\javalab>java p1 Error: Could not find or load main class p1 C:\javalab>
16 个解决方案
#1
26
I was having a similar issue with my very first java program.
我的第一个java程序也遇到了类似的问题。
I was issuing this command
我发出了这个命令。
java HelloWorld.class
Which resulted in the same error.
这导致了同样的错误。
Turns out you need to exclude the .class
结果你需要排除。class。
java HelloWorld
#2
11
Try:
试一试:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux)
当我遇到同样的问题时,使用Fedora (linux)
#3
8
Simple way to compile and execute java file.(HelloWorld.java doesn't includes any package)
编译和执行java文件的简单方法(HelloWorld)。java不包含任何包)
set path="C:\Program Files (x86)\Java\jdk1.7.0_45\bin"
javac "HelloWorld.java"
java -cp . HelloWorld
pause
#4
6
javac should know where to search for classes. Try this:
javac应该知道在哪里搜索类。试试这个:
javac -cp . p1.java
You shouldn't need to specify classpath. Are you sure the file p1.java exists?
您不需要指定类路径。你确定文件p1吗?java存在吗?
#5
4
I had almost the same problem, but with the following variation:
我几乎有同样的问题,但有以下的变化:
- I've imported a ready-to-use maven project into Eclipse IDE from PC1 (project was working there perfectly) to another PC2
- 我已经从PC1导入了一个现成的maven项目到Eclipse IDE中(项目在那里完美地工作)到另一个PC2。
- when was trying to run the project on PC 2 got the same error "Could not find or load main class"
- 当试图在PC 2上运行项目时,同样的错误“无法找到或装入主类”
- I've checked PATH variable (it had many values in my case) and added JAVA_HOME variable (in my case it was JAVA_HOME = C:\Program Files\Java\jdk1.7.0_03) After restarting Ecplise it still didn't work
- 我检查PATH变量(它在我的例子中有许多值)和添加JAVA_HOME变量(就我而言JAVA_HOME = C:\Program Files\Java\ jdk1.7.0_03)重启Ecplise后仍然没有工作
- I've tried to run simple HelloWorld.java on PC2 (in another project) - it worked
- 我试着简单地运行HelloWorld。java在PC2上(在另一个项目中)-它起作用了。
- So I've added HelloWorld class to the imported recently project, executed it there and - huh - my main class in that project started to run normally also.
- 因此,我已经将HelloWorld类添加到最近导入的项目中,并在那里执行它,并且——嗯——我在那个项目中的主要类也开始正常运行了。
That's quite odd behavour, I cannot completely understand it. Hope It'll help somebody. too.
这是很奇怪的行为,我不能完全理解。希望它能帮助别人。了。
#6
3
i guess that you have a different class name in p1.java
我想在p1.java中有一个不同的类名。
#7
3
Check you class name first. It should be p1 as per your batch file instruction. And then check you package of that class, if it is inside any package, specify when you run.
首先检查你的类名。它应该是p1,按照您的批处理文件指令。然后检查该类的包,如果它在任何包中,请在运行时指定。
If package is x.y
如果包是x.y
java x.y.p1
#8
3
Here is my working env path variables after much troubleshooting
这里是我的工作env路径变量,在许多故障排除之后。
CLASSPATH
类路径
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
C:\程序文件(x86)\Java\jre7\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ - \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \。
PATH <---sometimes this PATH fills up with too many paths and you can't add a path(which was my case!)
路径<——有时这条路径会填满太多路径,而您不能添加路径(这是我的情况!)
bunchofpaths;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
bunchofpaths;C:\ Program Files \ Java \ jdk1.6.0_27 \ bin(x86)
Additionally, when you try to use the cmd to execute the file...make sure your in the local directory as the file your trying to execute (which you did.)
另外,当您尝试使用cmd来执行文件时…确保您在本地目录中的文件是您想要执行的文件(您确实是这样做的)。
Just a little checklist for people that have this problem still.
只是给那些有这个问题的人一个小小的检查表。
#9
2
I've had similar problems. If you work with Eclipse, you need to go to the folder where you have your src/ folder... If you used a package - then you use
我有类似的问题。如果您使用Eclipse,您需要转到有src/文件夹的文件夹…如果您使用了一个包——那么您就使用它。
javac -cp . packageName/className
which means if you've had a package named def and main class with name TextFrame.java you'd write
这意味着如果您有一个名为def的包和一个带有名称TextFrame的主类。java你编写
javac -cp . def/TextFrame
omitting the trailing .java extension, and then you run it with the
省略后面的.java扩展,然后运行它。
java def/TextFrame
and if you have have arguments, then you need to supply it with arguments corresponding to your program. I hope this helps a bit.
如果你有参数,那么你需要向它提供与程序对应的参数。我希望这能有所帮助。
#10
1
First, put your file *.class
(e.g Hello.class
) into 1 folder (e.g C:\java
). Then you try command and type cd /d C:\java
. Now you can type "java Hello" !
首先,将文件*。类(e。将g hellol .class)装入1个文件夹(e)。g C:\ java)。然后尝试命令和类型cd / dc:\java。现在可以输入“java Hello”了!
#11
1
You might have the CLASSPATH environment variable already added!!
您可能已经添加了CLASSPATH环境变量!!
Use following to avoid further usage of -cp .
in java -cp . CLASSFILE
使用以下方法避免进一步使用-cp。在java - cp。类文件
Add . to CLASSPATH in system properties->environment variables or by cmd
添加。系统属性的类路径->环境变量或cmd。
set CLASSPATH=%CLASSPATH%;.;
设置CLASSPATH = % CLASSPATH %;;
#12
1
I faced a similar problem in Eclipse. Whenever I clicked on the Run button it gave me the message, "Error: Could not find or load main class". But when I right click on the java file in the project explorer and Run As Java configuration, it works perfectly.
我在Eclipse中遇到了类似的问题。每当我点击运行按钮,它就会给我提示:“错误:无法找到或加载主类”。但是,当我在project explorer中右键单击java文件并以java配置运行时,它就能正常工作了。
I think this is because it tries by default to run it in some other configuration which causes problems.
我认为这是因为它试图在其他的配置中运行它导致问题。
Hope this answer helps some.
希望这个答案能帮助一些人。
#13
1
If you have a single .java file to compile using command-line , then remove topmost package parts from the code, the compile again, it will work.
如果您有一个使用命令行编译的.java文件,然后从代码中删除最顶层的包部分,重新编译,它就会工作。
This worked for me.
这为我工作。
#14
0
Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:
有时可能导致这个问题的原因与主类无关。我不得不艰难地找到它,它是我移动的参考图书馆它给了我
Could not find or load main class xxx Linux
无法找到或装入主类xxx Linux ?
I just delete that reference and added again and it worked fine again.
我只是删除了那个引用,然后又添加了,它又恢复了正常。
#15
0
i had
我有
':'
“:”
in my project name e.g 'HKUSTx:part-2' renaming it 'HKUSTx-part-2' worked for me
在我的项目名称e。g 'HKUSTx:part-2“重命名”HKUSTx-part-2“为我工作”。
#16
-2
You can use NetBeans IDE which is free to download and use "Open Source". You can even do other programming language in this IDE. The latest of which it supports HTML5. This makes your programming easier. If you're not familiar with it choose a book that is NetBeans integrated like Sams Teach Yourself Java in 24 Hours
您可以使用免费下载和使用“开源”的NetBeans IDE。您甚至可以在这个IDE中使用其他编程语言。最新的支持HTML5。这使您的编程更加容易。如果你对它不熟悉,那就选一本书,像Sams一样的NetBeans在24小时内自学Java。
#1
26
I was having a similar issue with my very first java program.
我的第一个java程序也遇到了类似的问题。
I was issuing this command
我发出了这个命令。
java HelloWorld.class
Which resulted in the same error.
这导致了同样的错误。
Turns out you need to exclude the .class
结果你需要排除。class。
java HelloWorld
#2
11
Try:
试一试:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux)
当我遇到同样的问题时,使用Fedora (linux)
#3
8
Simple way to compile and execute java file.(HelloWorld.java doesn't includes any package)
编译和执行java文件的简单方法(HelloWorld)。java不包含任何包)
set path="C:\Program Files (x86)\Java\jdk1.7.0_45\bin"
javac "HelloWorld.java"
java -cp . HelloWorld
pause
#4
6
javac should know where to search for classes. Try this:
javac应该知道在哪里搜索类。试试这个:
javac -cp . p1.java
You shouldn't need to specify classpath. Are you sure the file p1.java exists?
您不需要指定类路径。你确定文件p1吗?java存在吗?
#5
4
I had almost the same problem, but with the following variation:
我几乎有同样的问题,但有以下的变化:
- I've imported a ready-to-use maven project into Eclipse IDE from PC1 (project was working there perfectly) to another PC2
- 我已经从PC1导入了一个现成的maven项目到Eclipse IDE中(项目在那里完美地工作)到另一个PC2。
- when was trying to run the project on PC 2 got the same error "Could not find or load main class"
- 当试图在PC 2上运行项目时,同样的错误“无法找到或装入主类”
- I've checked PATH variable (it had many values in my case) and added JAVA_HOME variable (in my case it was JAVA_HOME = C:\Program Files\Java\jdk1.7.0_03) After restarting Ecplise it still didn't work
- 我检查PATH变量(它在我的例子中有许多值)和添加JAVA_HOME变量(就我而言JAVA_HOME = C:\Program Files\Java\ jdk1.7.0_03)重启Ecplise后仍然没有工作
- I've tried to run simple HelloWorld.java on PC2 (in another project) - it worked
- 我试着简单地运行HelloWorld。java在PC2上(在另一个项目中)-它起作用了。
- So I've added HelloWorld class to the imported recently project, executed it there and - huh - my main class in that project started to run normally also.
- 因此,我已经将HelloWorld类添加到最近导入的项目中,并在那里执行它,并且——嗯——我在那个项目中的主要类也开始正常运行了。
That's quite odd behavour, I cannot completely understand it. Hope It'll help somebody. too.
这是很奇怪的行为,我不能完全理解。希望它能帮助别人。了。
#6
3
i guess that you have a different class name in p1.java
我想在p1.java中有一个不同的类名。
#7
3
Check you class name first. It should be p1 as per your batch file instruction. And then check you package of that class, if it is inside any package, specify when you run.
首先检查你的类名。它应该是p1,按照您的批处理文件指令。然后检查该类的包,如果它在任何包中,请在运行时指定。
If package is x.y
如果包是x.y
java x.y.p1
#8
3
Here is my working env path variables after much troubleshooting
这里是我的工作env路径变量,在许多故障排除之后。
CLASSPATH
类路径
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
C:\程序文件(x86)\Java\jre7\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ - \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \。
PATH <---sometimes this PATH fills up with too many paths and you can't add a path(which was my case!)
路径<——有时这条路径会填满太多路径,而您不能添加路径(这是我的情况!)
bunchofpaths;C:\Program Files (x86)\Java\jdk1.6.0_27\bin
bunchofpaths;C:\ Program Files \ Java \ jdk1.6.0_27 \ bin(x86)
Additionally, when you try to use the cmd to execute the file...make sure your in the local directory as the file your trying to execute (which you did.)
另外,当您尝试使用cmd来执行文件时…确保您在本地目录中的文件是您想要执行的文件(您确实是这样做的)。
Just a little checklist for people that have this problem still.
只是给那些有这个问题的人一个小小的检查表。
#9
2
I've had similar problems. If you work with Eclipse, you need to go to the folder where you have your src/ folder... If you used a package - then you use
我有类似的问题。如果您使用Eclipse,您需要转到有src/文件夹的文件夹…如果您使用了一个包——那么您就使用它。
javac -cp . packageName/className
which means if you've had a package named def and main class with name TextFrame.java you'd write
这意味着如果您有一个名为def的包和一个带有名称TextFrame的主类。java你编写
javac -cp . def/TextFrame
omitting the trailing .java extension, and then you run it with the
省略后面的.java扩展,然后运行它。
java def/TextFrame
and if you have have arguments, then you need to supply it with arguments corresponding to your program. I hope this helps a bit.
如果你有参数,那么你需要向它提供与程序对应的参数。我希望这能有所帮助。
#10
1
First, put your file *.class
(e.g Hello.class
) into 1 folder (e.g C:\java
). Then you try command and type cd /d C:\java
. Now you can type "java Hello" !
首先,将文件*。类(e。将g hellol .class)装入1个文件夹(e)。g C:\ java)。然后尝试命令和类型cd / dc:\java。现在可以输入“java Hello”了!
#11
1
You might have the CLASSPATH environment variable already added!!
您可能已经添加了CLASSPATH环境变量!!
Use following to avoid further usage of -cp .
in java -cp . CLASSFILE
使用以下方法避免进一步使用-cp。在java - cp。类文件
Add . to CLASSPATH in system properties->environment variables or by cmd
添加。系统属性的类路径->环境变量或cmd。
set CLASSPATH=%CLASSPATH%;.;
设置CLASSPATH = % CLASSPATH %;;
#12
1
I faced a similar problem in Eclipse. Whenever I clicked on the Run button it gave me the message, "Error: Could not find or load main class". But when I right click on the java file in the project explorer and Run As Java configuration, it works perfectly.
我在Eclipse中遇到了类似的问题。每当我点击运行按钮,它就会给我提示:“错误:无法找到或加载主类”。但是,当我在project explorer中右键单击java文件并以java配置运行时,它就能正常工作了。
I think this is because it tries by default to run it in some other configuration which causes problems.
我认为这是因为它试图在其他的配置中运行它导致问题。
Hope this answer helps some.
希望这个答案能帮助一些人。
#13
1
If you have a single .java file to compile using command-line , then remove topmost package parts from the code, the compile again, it will work.
如果您有一个使用命令行编译的.java文件,然后从代码中删除最顶层的包部分,重新编译,它就会工作。
This worked for me.
这为我工作。
#14
0
Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the:
有时可能导致这个问题的原因与主类无关。我不得不艰难地找到它,它是我移动的参考图书馆它给了我
Could not find or load main class xxx Linux
无法找到或装入主类xxx Linux ?
I just delete that reference and added again and it worked fine again.
我只是删除了那个引用,然后又添加了,它又恢复了正常。
#15
0
i had
我有
':'
“:”
in my project name e.g 'HKUSTx:part-2' renaming it 'HKUSTx-part-2' worked for me
在我的项目名称e。g 'HKUSTx:part-2“重命名”HKUSTx-part-2“为我工作”。
#16
-2
You can use NetBeans IDE which is free to download and use "Open Source". You can even do other programming language in this IDE. The latest of which it supports HTML5. This makes your programming easier. If you're not familiar with it choose a book that is NetBeans integrated like Sams Teach Yourself Java in 24 Hours
您可以使用免费下载和使用“开源”的NetBeans IDE。您甚至可以在这个IDE中使用其他编程语言。最新的支持HTML5。这使您的编程更加容易。如果你对它不熟悉,那就选一本书,像Sams一样的NetBeans在24小时内自学Java。