Java Eclipse 错误“无法找到或加载主类”

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

Eclipse error "Could not find or load main class"

javaeclipseclasspathmain

提问by G Boggs

I know there are many duplicates of this question, but I have looked at them all, and none of them have solved the issue.

我知道这个问题有很多重复,但我都看过了,但没有一个解决了这个问题。

I am trying to run a class that has a main function. I have cleaned the project, checked the classpath for '.', added the bin folder to the classpath under run configurations. I'm not sure what else to try because the class is certainly in the source folder.

我正在尝试运行一个具有主要功能的类。我已经清理了项目,检查了“.”的类路径,将 bin 文件夹添加到运行配置下的类路径。我不确定还可以尝试什么,因为该类肯定在源文件夹中。

Could someone please help me with this issue?

有人可以帮我解决这个问题吗?

package testIt;

public class MemoryVisualizerApp extends Application{

public static void main(String[] args) {
    launch(args);
}

//Setup the scene and launch with given properties
@Override
public void start(Stage primaryStage) throws IOException{
    Parent root = FXMLLoader.load(getClass().getResource("/MemoryVisualizer.fxml"));
    Scene scene = new Scene(root, 650, 300);
    //Set whether the screen should be re-sizable (possibly best size = default)
    primaryStage.setResizable(true);
    primaryStage.setMinHeight(300);
    primaryStage.setMinWidth(550);
    primaryStage.setTitle("Memory Usage");
    primaryStage.setScene(scene);
    scene.getStylesheets().add("testIt/MemoryVisualizer.css");
    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() 
    {
        public void handle(WindowEvent e){
              /*Currently the threads continue running after window is closed. Looking for
              a way to stop the app threads when window closed without stopping the program.*/
        }
    });
    primaryStage.show();
    primaryStage.show();
}

}

This code is located within a package, withing the src folder. It utilizes some JavaFX files that arent shown but that shouldnt be an issue.

此代码位于一个包中,包含 src 文件夹。它利用了一些未显示但不应该成为问题的 JavaFX 文件。

This is the error: Error: Could not find or load main class testIt.MemoryVisualizerApp

这是错误: 错误:无法找到或加载主类 testIt.MemoryVisualizerApp

回答by Kumar Abhinav

If you are facing similar problem,it has to do with the "Run Configurations" not working properly in Eclipse.

如果您遇到类似的问题,这与 Eclipse 中的“运行配置”无法正常工作有关。

error1

错误1

Please right clik on your java class containing the main method .Go to Run As-> Run Configurations and put the correct patrameters.Project should be the Project which contains the main class and Main class should be fuly qualified class name.See the snapshots below for clarity.It should wok fine then.Basically,you are creating a Run configuration and you can use that saved configuration for later use also.

请右键单击包含 main 方法的 java 类。转到 Run As-> Run Configurations 并放置正确的参数。Project 应该是包含主类的项目,Main 类应该是完全限定的类名。请参阅下面的快照为清楚起见。它应该可以正常工作。基本上,您正在创建一个运行配置,您也可以使用该保存的配置供以后使用。

selecting run configurationparameters to be entered correctly

选择运行配置参数输入正确

回答by Yani

Try to point the classpathentry output pathin the .classpathfile to target/classesThe classpath file is located in the project folder. Edit it manually.

尝试将文件中的classpathentry 输出路径指向路径文件位于项目文件夹中。手动编辑它。.classpathtarget/classes

In my case, it was like this before:

就我而言,以前是这样的:

classpathentry kind="output" path="bin"

classpathentry kind="output" path="bin"

I changed it to:

我把它改成:

classpathentry kind="output" path="target/classes"

classpathentry kind="output" path="target/classes"

回答by Niroshan Abeywickrama

Follow these steps :

按着这些次序 :

1)Delete your current run configuration. 2)Go to the main class and run it by right clicking the mouse-->Run As-->Java Application.

1) 删除您当前的运行配置。2)转到主类并通过右键单击鼠标--> Run As-->Java Application运行它

Then you will see an auto generated new run configuration.

然后你会看到一个自动生成的新运行配置。

回答by Joi Pu

I just encountered the same problem as yours and I followed Kumar's directions and it worked out. One thing to add: you need to make sure that after you go to the correct main class, check the "Classpath" and your project name should be existed under the "User Entries". If not, add your project to the directory then you're good to go.

我刚刚遇到了和你一样的问题,我按照 Kumar 的指示做了,结果解决了。要添加的一件事:您需要确保在转到正确的主类后,检查“类路径”并且您的项目名称应该存在于“用户条目”下。如果没有,请将您的项目添加到目录中,然后您就可以开始了。

回答by Brendan

Found This while searching, and it was after I updated my Java that the problem seemed to occur.

在搜索时发现了这个,在我更新我的 Java 之后,问题似乎发生了。

In Eclipse from your project:

在您项目的 Eclipse 中:

Right-click on your project
Click Properties
Java build path: Libraries; Remove the "JRE System Library[J2SE 1.4]"
Click Add Library -> JRE System Library
Select the new "Execution Environment" or Workspace default JRE

回答by Frank Jiao

If all the solution does not work for you, it may be caused by missing libraries:

如果所有解决方案都不适用于您,可能是由于缺少库造成的:

  1. Right click your project, Build Path --> Configure Build Path --> Java Build Path --> Libraries
  2. Remove the missing libraries
  3. Go to your main class and run it
  1. 右键单击您的项目, Build Path --> Configure Build Path --> Java Build Path --> Libraries
  2. 删除丢失的库
  3. 转到您的主类并运行它

回答by Yellaiah D

I too faced same problem.

我也面临同样的问题。

Simple solution is : delete project( not source files please). close eclipse--> go to project folder-- > remove classpath and project file .

简单的解决方案是:删除项目(请不要删除源文件)。关闭 eclipse--> 转到项目文件夹--> 删除类路径和项目文件。

then open Eclipse and create project in same location

然后打开Eclipse并在同一位置创建项目

回答by Edy Aguirre

you should add bin folder - run->run Configuration tab classpath advanced button - add folder - choose bin folder (yourproyect/bin)

您应该添加bin文件夹-运行->运行配置选项卡类路径高级按钮-添加文件夹-选择bin文件夹(yourproyect/bin)

回答by sguha

I had the same problem. In my case I did the following, to resolve it.

我有同样的问题。就我而言,我执行了以下操作来解决它。

  1. Select the project in the Eclipse IDE, right click, and open properties dialog.
  2. Select "Java Build Path" item on the left panel of the Properties dialog.
  3. Select the "Libraries" tab, on the right hand panel.
  4. Remove the libraries (jars), which are marked red, or fix the path.
  5. Save the Properties dialog. After removing the "Not found" libraries, Eclipse run/debug started working. The "Not found" message would also show up in the "Problems" pane. which you can open using, Window->Show View->Problems, command sequence. This pane gives you useful messages. The same message might show up for many reasons, other than what I encountered.
  1. 在 Eclipse IDE 中选择项目,右键单击并打开属性对话框。
  2. 在“属性”对话框的左侧面板上选择“Java 构建路径”项。
  3. 选择右侧面板上的“库”选项卡。
  4. 删除标记为红色的库(jar),或修复路径。
  5. 保存属性对话框。删除“未找到”库后,Eclipse 运行/调试开始工作。“未找到”消息也将显示在“问题”窗格中。您可以使用“窗口”->“显示视图”->“问题”、命令序列打开它。此窗格为您提供有用的消息。除了我遇到的原因之外,可能会出于多种原因显示相同的消息。

Observation: The classes directory in my case was not created, which means that the javac failed. On command line also, javac works the same way, if you have multiple java files in the same invocation of javac, and one java file encounters a problem, then no class files are created.

观察:在我的例子中没有创建 classes 目录,这意味着 javac 失败了。在命令行上,javac 的工作方式也是一样的,如果在同一个 javac 调用中有多个 java 文件,并且一个 java 文件遇到问题,则不会创建任何类文件。

Which means Eclipse puts all the java files in the project with single invocation of javac.

这意味着 Eclipse 将所有 java 文件放在项目中,只需调用一次 javac。

Now if there are no class files generated, obviously, the message now makes sense. The compile errors are also shown in the "Problems" pane.

现在,如果没有生成类文件,显然,该消息现在是有意义的。编译错误也显示在“问题”窗格中。

Now this would be the case if there is a compile error. If just a jar file which is not used at all, is on the classpath, there should not be any problem.

现在,如果存在编译错误,就会出现这种情况。如果只是一个根本不使用的 jar 文件在类路径上,应该没有任何问题。

Even if my project had problems, I ignored it, because I was just testing a single class, and was thinking that only that file would get compiled and run, because it was a standalone test, without any dependencies.

即使我的项目有问题,我也忽略了它,因为我只是在测试一个类,并且认为只有那个文件会被编译和运行,因为它是一个独立的测试,没有任何依赖项。

But that is not the case with Eclipse. I wish they did it that way. As you can see lot of people got burnt this way.

但 Eclipse 的情况并非如此。我希望他们这样做。正如你所看到的,很多人都是这样被烧死的。

Thanks

谢谢

回答by Tom Rusky

Removing the JRE System Library and adding the default one worked for me.

删除 JRE 系统库并添加默认库对我有用。