Java 错误:无法找到或加载主类测试

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

Java Error: Could not find or load main class Test

javavisual-studio-code

提问by Minimuscle

Searched and can't find a reason why this doesn't work. I get this error when trying to run a simple program:

搜索并找不到这不起作用的原因。尝试运行一个简单的程序时出现此错误:

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

I have used the gear to automatically generate the launch.json file. I have also never really done anything before. I'm using VSCode with the redhat java and java debugger extensions. I have java 1.8.0 installed (+jdk)

我已经用gear自动生成了launch.json文件。我之前也从未真正做过任何事情。我将 VSCode 与 redhat java 和 java 调试器扩展一起使用。我安装了 java 1.8.0 (+jdk)

launch.json code:

launch.json 代码:

{
"version": "0.2.0",
"configurations": [
    {
        "type": "java",
        "name": "Debug (Launch)-Test",
        "request": "launch",
        "cwd": "${workspaceFolder}",
        "stopOnEntry": false,
        "mainClass": "Test",
        "args": ""
    },
    {
        "type": "java",
        "name": "Debug (Attach)",
        "request": "attach",
        "hostName": "localhost",
        "port": 0
    }
]
}

采纳答案by Minimuscle

I installed java 9 and openjdk 9.0.1 and it now works. No idea why Java 8u151 didn't work, but this does now.

我安装了 java 9 和 openjdk 9.0.1,现在可以使用了。不知道为什么 Java 8u151 不起作用,但现在可以了。

回答by Animesh Jaiswal

I faced the same problem.While debugging in VS code 1.35.1it showed the errorCould not find or load main classin the Debug console.So I tried uninstalling and then reinstalling the latest version of VS code and now it is working fine.

我遇到了同样的问题。在调试时在调试控制台中VS code 1.35.1显示错误Could not find or load main class。所以我尝试卸载然后重新安装最新版本的 VS 代码,现在它工作正常。