Reputation: 83
I downloaded an existing project that works fine in eclipse/intelliJ etc. I want to make it work in visual studio code. I have downloaded the various extensions that vscode suggests when opening a .java file, I have configured the launch.json file to find my main class etc. I press F5 to launch the application and the error I get is:
Error: Could not find or load main class pacman.Executor
Caused by: java.lang.ClassNotFoundException: pacman.Executor
This is not the only error I have gotten, before this error appeared, I got this problem:
build project cannot be resolved to a type
at some classes that I am using in the main method. This error seems to have just disappeared and I don't know whether I'm further away from solving the problems or closer to, I cannot replicate this error.
My launch.json looks like following:
"configurations": [
{
"type": "java",
"name": "Debug (Launch)-Executor",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopOnEntry": false,
"mainClass": "pacman.Executor",
"args": ""
}
]
I am afraid that the issue might have something to do with the classpath, but I have tried several things with this such as setting it directly in the launch.json to my jdk and jre without luck.
Also, currently I'm not using maven or anything like that - is that necessary in order to build the project from within vscode when using redhat java plugin?
I hope someone can help me with my confusion or have an idea what the problem might be about.
Thanks in advance.
Upvotes: 4
Views: 8863
Reputation: 11
The class "NAME" in your code should be the same as the file "NAME" in which you are writing your code. That should sort your issue.
EDIT: I know that this thread is over 2 years old but I am still adding this answer as a lot people new to java in VSCode still face this issue so hopefully they'll see this solution which actually works.
Upvotes: 1
Reputation: 347
I think I found the way to fix this. All you need to do is to point to your .java file in your mainClass variable, for example not "something.Class" but "actualFilename.java". After I did this VSCode successfully located my main Class.
Upvotes: 0
Reputation: 11
I am not sure what the problem is exactly but you could make sure you are correctly compiling your Java files by adding a task to your workplace (by creating a tasks.json
file and putting it in your .vscode
folder) that launches before running your program (with the launch.json
code).
First, add the compile java task. In the following code, bin
is the classpath in relation to the ${workspaceFolder}
(cwd
is defaulted to the workspace folder). If the file is in the workspace folder, you can replace bin
with .
(current directory). ${fileDirname}\\*.java
includes all Java files in the current file's directory. This is particularly useful for when working with packages. If you'd prefer to just compile the current file, you can replace *
with the name of the file (e.g. pacman).
{
"version": "2.0.0",
"tasks": [
{
"label": "compile java",
"type": "shell",
"command": "javac -d bin -cp bin ${fileDirname}\\*.java",
"group": "build"
}
]
}
Second, add the preLaunchTask
parameter to your launch.json
code. It must be the same name you have given to the task (in this case, I named it "compile java"). This parameter forces the given task to be run before launching your code (before running the file).
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "run java",
"request": "launch",
"console": "internalConsole",
"mainClass": "pacman.Executor",
"preLaunchTask": "compile java", // <--- add the task here
"classPaths": ["bin"], // optional but try adding if still not working
}
]
}
I am not sure if this will solve your issue but I hope it somehow helps, nonetheless.
Some resources that you can referece for additional info:
Upvotes: 0