yelsayed
yelsayed

Reputation: 5542

Add current project to vscode launch.json "classpaths" field

I'm trying to debug my Java project in vscode. I added a launch.json file that looks like this:

  "configurations": [
    {
      "type": "java",
      "name": "Debug with args",
      "args": "--args_for_app",
      "classPaths": ["$HOME/bin/some.jar", "$HOME/bin/some_other.jar"],
      "env": {
        "env1": "env1value"
      },
      "request": "launch",
      "mainClass": "my.main.ClassName",
      "projectName": "projectName",
      "console": "internalConsole"
    }
  ]

If I remove the classPaths field, vscode automatically resolves the current project into the classpath, and the debugger starts successfully, but I need to also add other jar files into classpath. If I add the field with my desired jar paths, vscode no longer resolves the project's path automatically (which makes sense), but now I don't know how to add it back to the list.

I tried adding ".", "${file}", and "${workspace}", none worked. I still get an error saying the main class can't be found:

Error: Could not find or load main class my.main.ClassName
Caused by: java.lang.ClassNotFoundException: my.main.ClassName

Upvotes: 3

Views: 11834

Answers (1)

Barry Wang
Barry Wang

Reputation: 1469

So the main problem is to add jar as the dependency, right? Please have a look at the official doc: https://code.visualstudio.com/docs/java/java-project#_standalone-java-file-support

A function has been added to specify the jar files as the library.


Here is the edit, we can do the following to add jars/lib in VSCode:

  1. Add your .jar file into a ./lib in your project root.

  2. Configure paths in the .classpath to point to your jars, like:

<classpathentry kind="lib" path="lib/my.jar"/>
  1. If the jars are not picked up, clean the workspace directory. You can do this by running the "Clean the Java language server workspace" command. You can also manually clean the workspace directories:

The VS Code user workspace storage area can be found under these locations :

Windows : %APPDATA%\Code[ - Variant]\User\workspaceStorage\

MacOS : $HOME/Library/Application Support/Code[ - Variant]/User/workspaceStorage/

Linux : $HOME/.config/Code[ - Variant]/User/workspaceStorage/ could refer to add jar

Upvotes: 2

Related Questions