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

Time:03-31

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

Create Java project in VS Code and run the project.

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.

CodePudding user response:

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 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"
    }
  ]
}

CodePudding user response:

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.

  • Related