1

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)

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.

  • VS Code does not support Java without an additional extension. Did you install an extension for Java language support and if yes, which extension(s) exactly? With [Language Support for Java(TM) by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java) you will move from Eclipse to Eclipse with VS Code as UI. – howlger Mar 17 '22 at 12:02
  • Yes, I'm already installed Language Support for Java(TM) by Red Hat. – Paweenwat Maneechai Mar 17 '22 at 16:50
  • [Lol](https://twitter.com/howlger/status/1358189525424459777) (consider to improve your question). Do not ignore the problem which is shown in your screenshot. Delete the .class file in the `bin` folder and check whether it will be recreated when editing and saving the corresponding .java file. Look inside the .argfile that contains the arguments whether the `bin` folder is included on the classpath. – howlger Mar 18 '22 at 14:13
  • @howlger I'm tried to deleted all files in `bin` folder and run again, it's still has the same error and `class` files is not created. – Paweenwat Maneechai Mar 29 '22 at 11:07
  • When the `.class` files in the `bin` folder are not re-created, your project is not correct configured. – howlger Mar 29 '22 at 11:24
  • How could I reconfigure the Eclipse Java project again in VS Code? – Paweenwat Maneechai Mar 29 '22 at 11:27
  • Create a new Java project and compare the `.project` and `.classpath` file and the `.settings` folder with those of the existing project. – howlger Mar 29 '22 at 12:13
  • Finally I can run Java project with VS Code but only if the project location is in the Windows drive, in other drive such as D:, it still caused the error `java.lang.ClassNotFountException` because it build the `.argfiles` in `%TEMP%` folder, in the other hand, newly created java project from vs code can run even location is not in Windows drive or drive C:. – Paweenwat Maneechai Mar 30 '22 at 17:12

2 Answers2

2

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

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.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • 1
    Has your problem been solved? I think I need more information to help you deal with the problem – MingJie-MSFT Mar 24 '22 at 06:26
  • Sorry, I'm moved back to Eclipse for a while, after I followed this [instuction](https://code.visualstudio.com/docs/java/java-tutorial) I can create Java project in VS Code and run properly, but if I run Java project from Eclipse, It's still has a same problem. – Paweenwat Maneechai Mar 29 '22 at 11:01
  • I think we can start with the reason for the error "ClassNotFound". Since vscode can be created and run normally, I think this may be caused by some different configurations between eclipse and vscode. We can exclude and analyze them one by one according to the content provided in this article :https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean/18093929#18093929 – MingJie-MSFT Mar 30 '22 at 02:12