Search code examples
javajarvscode-debugger

How to debug code by stepping into jar file code for which I have the source code in Visual Studio Code?


I am a C#, c++ developer. If I have the code and pdbs to external dlls, I am able to step into the code of the external dll, by pointing IDE to the code and/or the PDBs. I want to do something similar with Java JARs

I have created a Micro Service framework in java using Visual Studio code. This compiles into a jar file. I have another application that is consuming this jar file. When I am debugging my application, how can I step into the code of the Micro Service jar?

I am using VS Code 1.45.1, zulu 14, maven 3.6.3

Say I have a jar file called MyMaths.jar, inside it there is a class mybasicmaths

    public class MyBasicMaths{
       public int addNums(int a, int b) {
           return a+b;
       }
    }

This jar is being used by another application consuming the MyMaths.jar. I have resolved the dependencies using Maven. In the client application, I have code like

    public class MyMathsConsumer {
       public static void main(String[] args) {
         int a = 10;
         int b = 20
         MyBasicMaths myObj = new myBasicMaths();
         int c = myObj.addNums(10, 20);
         System.out.println(c);
       }
    }

I am able to run my project fine. But I want to be able to stepinto the code of MyBasicMaths.addNums() from MyMathsConsumer.

When I am debugging, I am able to step into amqp-client-5.7.3.jar and even zulu classes, but not into the jar file I have created.

Similar question has been asked for eclipse Attach the Source in Eclipse of a jar

I am asking the same for Visual Studio Code.


Solution

  • (from https://code.visualstudio.com/docs/java/java-project)

    By default, a referenced {binary}.jar will try to search {binary}-sources.jar under the same directory, and attach it as source if one match is found.

    If you want to manually specify a JAR file as a source attachment, you can provide a key-value map in the sources field:

    "java.project.referencedLibraries": {
        "include": [
            "library/**/*.jar",
            "/home/username/lib/foo.jar"
        ],
        "exclude": [
            "library/sources/**"
        ],
        "sources": {
            "library/bar.jar": "library/sources/bar-src.jar"
        }
    }