“无法找到或加载主类”使用 cmd 提示符运行 java 程序时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23530173/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:38:33  来源:igfitidea点击:

"Could not find or load main class" Error while running java program using cmd prompt

javacmdclasspathenvironment-variables

提问by user241702

I am running a simple "HelloWorld" Program. I get this error in the command prompt:

我正在运行一个简单的“HelloWorld”程序。我在命令提示符中收到此错误:

Could not find or load main class HelloWorld.

无法找到或加载主类HelloWorld

I have set the CLASSPATHand PATHvariable in the system. In the cmdprompt, I am running from the directory where I have saved HelloWorldprogram. I can see the class name and the file name are same and also .classfile created in the same directory. What else could be the problem?

我已经在系统中设置了CLASSPATHPATH变量。在cmd提示中,我从保存HelloWorld程序的目录运行。我可以看到类名和文件名是相同的,并且.class在同一目录中创建了文件。还有什么问题?

My sample program looks like this:

我的示例程序如下所示:

package org.tij.exercises;
public class HelloWorld {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello World!!");
  }
}

回答by Kakarot

When the Main class is inside a package then you need to run it as follows :

当 Main 类位于包内时,您需要按如下方式运行它:

java <packageName>.<MainClassName>

In your case you should run the program as follows :

在您的情况下,您应该按如下方式运行程序:

java org.tij.exercises.HelloWorld 

回答by youngzy

What's your CLASSPATHvalue?

你的CLASSPATH价值是什么?

It may look like this:

它可能看起来像这样:

 .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar 

I guess your value does not contain this .;.

我猜你的价值不包含这个.;

So, ADD IT.
When you done , restartCMD

所以,添加它
完成后,重新启动CMD

That may works.

这可能有效。



For example the file HelloWorld.javais in path: D:\myjavatest\org\yz\testand its packageis: org.yz.test.

例如,文件HelloWorld.java在 path: 中D:\myjavatest\org\yz\test,它package是:org.yz.test

Now, you're in path D:\myjavatest\on the CMD line.
Type this to compile it:

现在,您在D:\myjavatest\CMD 行的路径中。
输入这个来编译它:

javac org/yz/test/HelloWorld.java

Then, type this to run it:

然后,键入以下内容以运行它:

java org.yz.test.HelloWorld

You may get what you want.

你可能会得到你想要的。

回答by user241702

I removed binfrom the CLASSPATH. I found out that I was executing the javacommand from the directory where the HelloWorld.javais located, i.e.:

binCLASSPATH. 我发现我是java从所在目录执行命令的HelloWorld.java,即:

C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld

C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld

So I moved back to the main directory and executed:

所以我移回主目录并执行:

java org.tij.exercises.HelloWorld

java org.tij.exercises.HelloWorld

and it worked, i.e.:

它起作用了,即:

C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld

C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld

Hello World!!

你好,世界!!

回答by Vishrant

Execute your Java program using java -d . HelloWorldcommand.

使用java -d . HelloWorld命令执行您的 Java 程序。

This command works when you have declared package.

当您声明 package.json 时,此命令有效。

.represent current directory/.

.代表当前目录/。

回答by Adnan

One reason for this error might be

此错误的一个原因可能是

Could not find or load main class <class name>

无法找到或加载主类 <class name>

Maybe you use your class name as different name and save the class name with another name you can save a java source file name by another name than class name. For example:

也许您使用您的类名作为不同的名称并用另一个名称保存类名,您可以使用类名以外的其他名称保存java源文件名。例如:

class A{
    public static void main(String args[]) {
        System.out.println("Hello world");
    }
}

you can save as Hello.javabut,

你可以另存为Hello.java但是,

To Compile : javac Hello.java

编译 : javac Hello.java

This will auto generate A.class file at same location.

这将在同一位置自动生成 A.class 文件。

Now To Run        : java A

现在运行: java A

回答by decoder

I faced the same problem and tried everything mentioned here. The thing was I didn't refresh my project in eclipse after class creation . And once I refreshed it things worked as expected.

我遇到了同样的问题并尝试了这里提到的所有内容。问题是我没有在创建类后在 Eclipse 中刷新我的项目。一旦我刷新它,事情就会按预期进行。

回答by outlooker

I had the same problem, mine was a little different though I did not have a package name. My problem was the Class Path for example:

我有同样的问题,虽然我没有包名,但我的有点不同。我的问题是类路径,例如:

C:\Java Example>java -cp . HelloWorld 

The -cpoption for Java and from what I can tell from my experience (not much) but I encountered the error about 20 times trying different methods and until I declared the class Path I was receiving the same error. Vishrant was correct in stating that . represents current directory.

-cpJava的选项以及我从我的经验中可以看出的内容(不多),但是我尝试不同的方法时遇到了大约 20 次错误,直到我声明了类路径,我才收到相同的错误。Vishrant 的说法是正确的。代表当前目录。

If you need more information about the java options enter java -?or java -helpI think the options are not optional.

如果您需要有关 java 选项的更多信息,请输入java -?或者java -help我认为这些选项不是可选的。

I just did some more research I found a website that goes into detail about CLASSPATH. The CLASSPATHmust be set as an environment variable; to the current directory <.>. You can set it from the command line in windows:

我只是做了一些更多的研究,我找到了一个详细介绍CLASSPATH. 在CLASSPATH必须被设置为环境变量; 到当前目录 <.>。您可以从 Windows 中的命令行设置它:

// Set CLASSPATH to the current directory '.'
prompt> set CLASSPATH=.

When you add a new environment setting you need to reboot before enabling the variable. But from the command prompt you can set it. It also can be set like I mentioned at the beginning. For more info, and if your using a different OS, check: Environment Variables.

添加新环境设置时,您需要在启用变量之前重新启动。但是从命令提示符您可以设置它。它也可以像我开头提到的那样设置。有关更多信息,如果您使用不同的操作系统,请检查:环境变量

回答by kenorb

Since you're running it from command prompt, you need to make sure your classpathis correct. If you set it already, you need to restart your terminal to re-load your system variables.

由于您是从命令提示符运行它,因此您需要确保您的类路径是正确的。如果您已经设置了它,则需要重新启动终端以重新加载系统变量。

If -classpathand -cpare not used and CLASSPATHis not set, the current directory is used (.), however when running .classfiles, you need to be in the folder which consist Java package namefolders.

如果-classpath-cp不使用CLASSPATH且未设置,则使用当前目录(.),但是运行.class文件时,您需要在包含Java包名称文件夹的文件夹中。

So having the .classfile in ./target/classes/com/foo/app/App.class, you've the following possibilities:

因此,将.class文件放入 中./target/classes/com/foo/app/App.class,您有以下可能性:

java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App

You can check your classpath, by printing CLASSPATHvariable:

您可以通过打印CLASSPATH变量来检查您的类路径:

  • Linux: echo $CLASSPATH
  • Windows: echo %CLASSPATH%
  • Linux: echo $CLASSPATH
  • 视窗: echo %CLASSPATH%

which has entries separated by :.

其中条目由:.

See also: How do I run Java .class files?

另请参阅:如何运行 Java .class 文件?

回答by pev.hall

I had a similar problem when running java on win10

我在 win10 上运行 java 时遇到了类似的问题

instead of

代替

$ java ./hello
Error: Could not find or load main class ..hello

Run

$ java hello
Hello, World

回答by Maria

faced the same problem. solved by following these steps

面临同样的问题。按照以下步骤解决

  • go to directory containing the package 'org.tij.exercises' (e.g: in eclipse it may be your src folder)
  • use java org.tij.exercises.HelloWorld
  • 转到包含“org.tij.exercises”包的目录(例如:在 eclipse 中,它可能是您的 src 文件夹)
  • java org.tij.exercises.HelloWorld