13

I'm trying to make VSCode work with using a Gradle build file using Java with modules (from Java version 11). The gradle.build compile works just fine, but I cannot make VSCode execute the output compiled Java class files. (It always tries to create its own.)

VSCode version: 1.48.2

System Info: Ubuntu 2020.04

Java Build:

openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

build.gradle file:

apply plugin: 'java'
def version = 0.1

sourceSets {
    main {
        java {
            srcDir 'src/easytext/javamodularity/easytext'
        }
    }
}

module-info.java located at $projectDir/src/easytext/module-info.java

// module easytext {
module easytext.javamodularity.easytext {
    requires java.base;
}

Really simple Main.java, located at $projectDir/src/easytext/javamodularity/easytext/Main.java:

//package easytext;
package easytext.javamodularity.easytext;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            System.out.println("Welcome to EasyText. Please provide a filename as input argument.");
            return;
        }

}

The gradle file always wants to output to $projectDir/build/, but VScode always wants to run the files in $projectDir/bin/easytext/javamodularity/easytext/Main.class.

I am also getting the VSCode error which I do not understand: "module-info.java is not on the classpath of project EasyText, only syntax errors are reported."

Running the Gradle task 'build' does compile successfully.

I do not have a launch.json file because I am a loss for how to point it to the modulePath? Or is there some sort of magic I'm missing?

2 Answers 2

9

If you are properly configured to debug gradle tasks, you will see a debug icon next to the run icon in the gradle Tasks list. To get to the gradle task list, click on the Gradle logo in the extensions bar (indicated by yellow arrow). The red arrow shows the debug task.

enter image description here

VSCode is not necessarily configured to display this option. There are 3 places this can be configured.

  1. The User settings.json file.
  2. The Workspace settings.json file.
  3. The remote machine's settings.json file. (Only applicable if developing on a remote server)

These files can be edited by

  1. Pressing Ctrl+, to open the settings window.
  2. Type gradle.javaDebug in the search bar.
  3. Select User, Remote[SSH:yourhosthere], or Workspace as desired.
  4. Click Edit in settings.json
  5. Add the following code inside the top level curly brackets.

Note 1: While typing, you will get the option to auto complete the rest of the configuration. Additionally, if there are other entries in your settings.json file, make sure you include a comma as appropriate.

{
  "gradle.javaDebug": {
    "tasks": [
      "run",
      "runBoot",
      "test",
      "intTest",
      "integration"
    ]
  }
}

Note 2: If you have a subproject, you should add the "gradle.javaDebug" entry into the Workspace settings.json. The entry would look something like the following.

{
  "gradle.javaDebug": {
    "tasks": [
      "run",
      "runBoot",
      "test",
      "intTest",
      "integration",
      "subprojectname:run",
      "subprojectname:test"
    ]
  }
}

This was tested using Visual Studio Code Version: 1.61.2. If the changes to the settings.json do not fix your problem, there is an open issue (as of the time of this posting) that may be related to your issue. https://github.com/microsoft/vscode-gradle/issues/876

4

This is an old question but i had the same problem, here is my solution if anyone needs it.

1)Install the gradle vscode extension

2)open the gradle tasks and here you have the run/debug options enter image description here

Alternatively you could run the task from terminal using

Linux

./gradlew build

./gradlew run

Windows

gradlew.bat build

gradlew.bat run

Not the answer you're looking for? Browse other questions tagged or ask your own question.