错误; 无法找到或加载主类(Java 使用 Windows CMD)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25492996/
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 (Java using Windows CMD)
提问by Keir Simmons
I am trying to compile and run some java files I have made in Eclipse. The full path to the .java file is C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java
. tests
is the package I created in Eclipse and src
is a folder that Eclipse made under Introduction
(which is the project name).
我正在尝试编译和运行我在 Eclipse 中制作的一些 java 文件。.java 文件的完整路径是C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java
. tests
是我在 Eclipse 中创建的包,src
是 Eclipse 在其下创建的文件夹Introduction
(这是项目名称)。
In my environment variables, I have the following relevant variable:
在我的环境变量中,我有以下相关变量:
JAVA_HOME
C:\Program Files (x86)\Java\jdk1.7.0_40\bin
JAVA_HOME
C:\Program Files (x86)\Java\jdk1.7.0_40\bin
Under system variables I have the following:
在系统变量下,我有以下内容:
CLASSPATH
%JAVA_HOME%
CLASSPATH
%JAVA_HOME%
I go to my cmd and cd
into the tests
directory (cd C:\Users\MYNAME\Documents\Java\Introduction\src\tests
). Then I compile using javac Test.java
. This seems to work as I then have a Test.class
file under the same directory. Now I want to run the file, I type java Test
and I get the error, "could not find or load main class". I've tried a variety of things including appending .class
and .java
to the end but I keep getting the error. I looked at some answers and docs and I managed to get it to work if I cd
into:
我转到我的 cmd 并cd
进入tests
目录 ( cd C:\Users\MYNAME\Documents\Java\Introduction\src\tests
)。然后我使用javac Test.java
. 这似乎有效,因为我Test.class
在同一目录下有一个文件。现在我想运行该文件,我输入java Test
并收到错误消息“无法找到或加载主类”。我尝试了各种方法,包括追加.class
和.java
最后,但我不断收到错误消息。我查看了一些答案和文档,如果我cd
进入,我设法让它工作:
cd C:\Users\MYNAME\Documents\Java\Introduction\src
(i.e, get out of the package)
cd C:\Users\MYNAME\Documents\Java\Introduction\src
(即,从包装中取出)
and then run:
然后运行:
java -cp . tests.Test
java -cp . tests.Test
So that seems to temporarily set the class path to the current directory, and run Test from the package tests
. However, I want to simply be able to type java Test
. I know it's possible as I used to be able to do it, but now for some reason I cannot (I must have changed something along the way...).
所以这似乎暂时将类路径设置为当前目录,并从包中运行测试tests
。但是,我只想能够输入java Test
. 我知道这是可能的,因为我曾经能够做到,但现在由于某种原因我不能(我一定在此过程中改变了一些东西......)。
Any help is appreciated.
任何帮助表示赞赏。
回答by Jon Skeet
However, I want to simply be able to type
java Test
但是,我只想能够输入
java Test
That will onlywork if Test
is in the default package - it's as simple as that. You need to pass the java
executable the fully-qualified name of the class you want to launch. There's no way round that.
这将仅如果工作Test
在默认的包-它的那样简单。您需要将java
要启动的类的完全限定名称传递给可执行文件。没有办法解决这个问题。
Of course, you could create your ownlauncher which looks in the current directory for class files, finds out the fully-qualified name of the classes within those files, and launches java
providing the full name and probably specifying an appropriate classpath... but that seems like a lot of hassle compared with just including the package name in the command.
当然,您可以创建自己的启动器,在当前目录中查找类文件,找出这些文件中类的完全限定名称,然后启动java
提供全名并可能指定适当的类路径......与仅在命令中包含包名称相比,这似乎很麻烦。
回答by Sajir
You could be making the same mistake I made. So, try the following.
你可能犯了和我一样的错误。因此,请尝试以下操作。
Here is my code for your reference.
这是我的代码供您参考。
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Once you saved this as "C:\JavaStudy\ClassA.java", try the following.
将其保存为“C:\JavaStudy\ClassA.java”后,请尝试以下操作。
c:\JavaStudy>javac ClassA.java
c:\JavaStudy>java A.class
Error: Could not find or load main class A.class
c:\JavaStudy>java A
Hello world
c:\JavaStudy>
Note: You don't need to use " java.exe -cp . " if you have class file in the same directory from where you are executing.
注意:如果您在执行的同一目录中有类文件,则不需要使用“ java.exe -cp ”。