coc-java icon indicating copy to clipboard operation
coc-java copied to clipboard

Gradle Project - Cannot be resolved to a module error

Open Equiphract opened this issue 3 years ago • 0 comments

I am a first time user of this extension and I wanted to create a Java desktop application using JavaFX. I am using Gradle to build the project and resolve dependencies.

The problem is that when creating a module-info.java file like so:

module me.equiphract.myproject {
  requires javafx.graphics;
}

the LSP is telling me that javafx.graphics cannot be resolved to a module.

Now, I created a Java file containing the main class of the application and referenced some of the classes in the javafx.graphics module like so:

package me.equiphract.myproject;

import javafx.application.Application;
import javafx.stage.Stage;

public class MyApplication extends Application {

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) throws Exception { }

}

And I am able to build the project without any errors using gradle build. Naturally all the references to library classes and methods are marked as errors by the LSP, as it is not able to see them for some reason. Funnily enough when using the "go to definition" action on, for example, Application, it actually takes me to that library source file.

For the sake of completeness, here is the project's build.gradle:

plugins {
  id 'application'
  id 'org.openjfx.javafxplugin' version '0.0.13'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.vladsch.flexmark:flexmark-all:0.64.0'
  testImplementation 'org.mockito:mockito-core:4.6.1'
  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

test {
  useJUnitPlatform()
}

application {
  mainModule = 'me.equiphract.myproject'
  mainClass = 'me.equiphract.myproject.MyApplication'
}

javafx {
  version = '20-ea+1'
  // note: javafx.graphics, which I reference above, is required by javafx.controls
  // and is implicitly downloaded as dependency
  modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
}

mainClassName = 'me.equiphract.myproject.MyApplication'

What is happening here?

Edit: I just found out that when I am not using a module-info.java file, which means not using the Java Module System, everything seems to work fine. But this is only a temporary fix as I want to be able to use modules.

Equiphract avatar Aug 14 '22 00:08 Equiphract