'VSC wrong java dependencies

I have a strange behaviour in Visual Studio Code, trying to debug a simple code with the VSC Debugger I have a runtime error, running the same code with Gradle the code works !

The problem is that running the code in VSC Debug mode the java code try to load two library, the commons-cli-1.2.jar and the new commons-cli-1.5.0.jar, in the old one some methods are not implemented and the code rise a java.lang.NoSuchMethodError

[144.386s][info][class,load] java.lang.Throwable$WrappedPrintStream source: jrt:/java.base
java.lang.NoSuchMethodError: 'boolean org.apache.commons.cli.Options.hasShortOption(java.lang.String)'
    at org.apache.commons.cli.DefaultParser.handleShortAndLongOption(DefaultParser.java:473)
    at org.apache.commons.cli.DefaultParser.handleToken(DefaultParser.java:542)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:712)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:679)
    at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:660)
    at it.fox.dafne.App.main(App.java:38)

The java code is:

  public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("f", PROPERTY_FILE, true, "path to the properties file");
    try {
      var parser = new DefaultParser();
      CommandLine commandLine = parser.parse(options, args);
      String propertyFile = "";
      ...
      ...

In the ClassPath there's no indication of the old library:

   <classpathentry  sourcepath="/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.5.0/e7e198ff537780c2835e484b0ffccd2960074504/commons-cli-1.5.0-sources.jar" kind="lib"  path="/Users/fox/.gradle/caches/modules-2/files-2.1/commons-   cli/commons-cli/1.5.0/dc98be5d5390230684a092589d70ea76a147925c/commons-cli-1.5.0.jar">

So, how is it possible for Java to find the Old version?

Visual Studio Code isn't supposed to use the java classPath which is in .classpath file ?

Enabling -verbose:class when the JVM is launched, the error is pretty clear:

[7.861s][info][class,load] org.apache.commons.cli.Options source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.862s][info][class,load] org.apache.commons.cli.Option source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.864s][info][class,load] org.apache.commons.cli.OptionValidator source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.867s][info][class,load] org.apache.commons.cli.CommandLineParser source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.867s][info][class,load] org.apache.commons.cli.DefaultParser source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.5.0/dc98be5d5390230684a092589d70ea76a147925c/commons-cli-1.5.0.jar
[7.868s][info][class,load] org.apache.commons.cli.ParseException source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.868s][info][class,load] org.apache.commons.cli.MissingArgumentException source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.869s][info][class,load] org.apache.commons.cli.MissingOptionException source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.870s][info][class,load] org.apache.commons.cli.UnrecognizedOptionException source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.870s][info][class,load] org.apache.commons.cli.AmbiguousOptionException source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.5.0/dc98be5d5390230684a092589d70ea76a147925c/commons-cli-1.5.0.jar
[7.871s][info][class,load] org.apache.commons.cli.CommandLine source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
[7.872s][info][class,load] org.apache.commons.cli.Util source:  file:/Users/fox/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar

In Gradle the dependencies are fine:

MacBook-Pro:plcProject fox$ gradle dafne:dependencies | grep commons-cli
|  |  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |     +--- commons-cli:commons-cli:1.2 -> 1.5.0
+--- commons-cli:commons-cli:1.5.0
+--- commons-cli:commons-cli:1.5.0 (n)
+--- commons-cli:commons-cli:1.5.0 (n)
|  +--- commons-cli:commons-cli:1.5.0
|  |  |  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |  |     +--- commons-cli:commons-cli:1.2 -> 1.5.0
+--- commons-cli:commons-cli:1.5.0
|  |  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |     +--- commons-cli:commons-cli:1.2 -> 1.5.0
+--- commons-cli:commons-cli:1.5.0
|  +--- commons-cli:commons-cli:1.5.0
|  |  |  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |  |  |  +--- commons-cli:commons-cli:1.2 -> 1.5.0
|  |  |  |     +--- commons-cli:commons-cli:1.2 -> 1.5.0
+--- commons-cli:commons-cli:1.5.0

Gradle find some transitive dependencies but decide to use the newer version of the library and not the old one:

So the questions are:

  • VSC ClassPath is the Gradle one?

  • How could be possible that launching the code with Gradle is working and with VSC not ?

Thanks,

S.



Solution 1:[1]

It seems that the code is fine.

The problem was solved cleaning up the Visual Studio Code cache where probably something was wrong. To clear the cache you have to use the "Command Palette" (??P, F1 on Mac; Ctrl+Shift+P, F1 on Windows) and use the command: "Java: Clean Java Server Language Workspace"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefano Bossi