Issue
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)
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.
Solution
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"
}
]
}
Answered By - Paweenwat Maneechai
Answer Checked By - Mildred Charles (JavaFixing Admin)