简体   繁体   中英

Cannot run Eclipse Java project in visual studio code (Could not find or load main class)

I want to move from Eclipse to Visual Studio Code, I can open project completely. Visual Studio Code can recognize .classpath and vice versa.

Visual Studio Code can recognize Java project as same as Eclispse do.

Then I go to the java file with Main method and run. It show error as

Error: Could not find or load main class com.untitled.game.Game Caused by: java.lang.ClassNotFoundException: com.untitled.game.Game

I never install Code Runner and I tried to clean workspace by cleaning the java server workspace, and nothing work. It still have a same problem.

However, this project can run in Eclipse before and never have a problem.

PS. After I followed this instuction. I can create Java project in VS Code and run properly. But if I run Java project from Eclipse, it's still has the same problem. (java.lang.ClassNotFoundException)

在 VS Code 中创建 Java 项目并运行该项目。

PS.1 If my Java project is not in Windows drive, the VS Code stills shows an error althougth reconfigured .setting as same as Java project that freshly created from VS Code.

Finally, I can run the Eclipse java project in vscode with settings from .vscode folder.

First, open Java project folder normally.

Then creates folder .vscode at the root of project and creates two files, settings.json and launch.json .

In settings.json file, add project configurations as follows.

{
    "java.project.sourcePaths": ["src"],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}

Run the project again, you will see an error, don't worry, vscode will add the configuration automatically. In launch.json file, you will see the configurations like this.

{
  "configurations": [
    {
      "type": "java",
      "name": "Launch Game",
      "request": "launch",
      "mainClass": "com.untitled.game.Game",
      "projectName": "Alien Hunter",
    }
  ]
}

In case of running the project that the location is outside from Windows drive, it still has an error because it shortened the -cp "path" command into the .argfile that saved in %temp% folder and called when running the project.

Add configuration to fix the problem.

"shortenCommandLine": "none"

If there have a native library in Eclipse, in case of my project, I'm using LWJGL , I will add configuration as follows.

"vmArgs": "-Djava.library.path=lib/lwjgl/native/windows"

If the project has the resources in another folder outside of src folder, for example, res folder, right-click on the folder and select Add Folder to Java Source Path , the project can be able to access the resource files.

launch.json

{
  "configurations": [
    {
      "type": "java",
      "name": "Launch Game",
      "request": "launch",
      "mainClass": "com.untitled.game.Game",
      "projectName": "Alien Hunter",
      "vmArgs": "-Djava.library.path=lib/lwjgl/native/windows",
      "shortenCommandLine": "none"
    }
  ]
}

Welcome to SO.Are you just starting to use vscode? If so, please check whether the Java interpreter is selected correctly, whether several necessary extensions are installed, and the necessary extensions are mentioned in this article . I hope they can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM