[英]Could not find or load main class
I have Windows 7, installed jdk1.7.0 and its supporting jre7. 我有Windows 7,安装了jdk1.7.0及其支持的jre7。
My problem is compilation part works perfectly, but while running the Java program I get this error saying: 我的问题是编译部分工作正常,但在运行Java程序时我收到此错误说:
"Could not find or load main class" “无法找到或加载主类”
I am storing all my programs in javalab folder. 我将所有程序存储在javalab文件夹中。 I have set the path to it. 我已经设定了它的路径。 Procedure looks like this: 过程如下所示:
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>
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
Try: 尝试:
java -cp . p1
This worked for me when I had the same problem, using Fedora (linux) 当我遇到同样的问题时,这对我有用,使用Fedora(linux)
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
javac should know where to search for classes. javac应该知道在哪里搜索课程。 Try this: 试试这个:
javac -cp . p1.java
You shouldn't need to specify classpath. 您不应该指定classpath。 Are you sure the file p1.java exists? 你确定文件p1.java存在吗?
I had almost the same problem, but with the following variation: 我有几乎相同的问题,但有以下变化:
That's quite odd behavour, I cannot completely understand it. 这是非常奇怪的行为,我无法完全理解它。 Hope It'll help somebody. 希望它会对某人有所帮助。 too. 太。
Here is my working env path variables after much troubleshooting 经过多次故障排除后,这是我工作的env路径变量
CLASSPATH CLASSPATH
.;C:\\Program Files (x86)\\Java\\jre7\\lib\\ext\\QTJava.zip;C:\\Program Files (x86)\\Java\\jdk1.6.0_27\\bin 。; C:\\ Program Files(x86)\\ Java \\ jre7 \\ lib \\ ext \\ QTJava.zip; C:\\ Program Files(x86)\\ Java \\ jdk1.6.0_27 \\ bin
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(x86)\\ Java \\ jdk1.6.0_27 \\ bin
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. 对于仍有这个问题的人来说,只需要一个小清单。
我想你在p1.java
有一个不同的类名
Check you class name first. 首先检查你的班级名称。 It should be p1 as per your batch file instruction. 根据您的批处理文件说明,它应该是p1。 And then check you package of that class, if it is inside any package, specify when you run. 然后检查该类的包,如果它在任何包中,请指定何时运行。
If package is xy
如果包是xy
java x.y.p1
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. 我希望这能有所帮助。
You might have the CLASSPATH environment variable already added!! 您可能已经添加了CLASSPATH环境变量!
Use following to avoid further usage of -cp .
使用以下命令以避免进一步使用-cp .
in java -cp . CLASSFILE
在java -cp . CLASSFILE
java -cp . CLASSFILE
Add . 添加。 to CLASSPATH in system properties->environment variables or by cmd 到系统属性 - >环境变量中的CLASSPATH或cmd
set CLASSPATH=%CLASSPATH%;.;
I faced a similar problem in Eclipse. 我在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. 但是当我右键单击项目资源管理器中的java文件和Run As 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. 希望这个答案有所帮助。
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. 这对我有用。
First, put your file *.class
(eg Hello.class
) into 1 folder (eg C:\\java
). 首先,将您的文件*.class
(例如Hello.class
)放入1个文件夹(例如C:\\java
)。 Then you try command and type cd /d C:\\java
. 然后你尝试命令并输入cd /d C:\\java
。 Now you can type "java Hello" ! 现在你可以输入“java Hello”了!
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. 我只是删除了该引用并再次添加,它再次正常工作。
i had 我有
':' ':'
in my project name eg 'HKUSTx:part-2' renaming it 'HKUSTx-part-2' worked for me 在我的项目名称中,例如'HKUSTx:part-2'重命名'HKUSTx-part-2'为我工作
You can use NetBeans IDE which is free to download and use "Open Source". 您可以使用可以免费下载并使用“开源”的NetBeans IDE 。 You can even do other programming language in this IDE. 您甚至可以在此IDE中执行其他编程语言。 The latest of which it supports HTML5. 最新的支持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的书,如24小时内的Sams Teach Yourself Java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.