Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems running a java project using vscode

Tags:

java

vs-code

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.

like image 535
Peterrr Avatar asked Sep 17 '26 22:09

Peterrr


1 Answers

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.

like image 136
freakcoder Avatar answered Sep 20 '26 00:09

freakcoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!