O
O
opopsel_86x642021-11-27 17:49:47
gradle
opopsel_86x64, 2021-11-27 17:49:47

How to properly configure build.gradle file to run javafx application?

I am trying to create an executable javafx application without using the javafx plugin.

plugins {
  id 'application'
}

repositories {
    mavenCentral()
flatDir{
    dirs 'C:/Users/insec/Desktop/javafx-sdk-17.0.1/lib'
  }
}

dependencies{
  implementation files ('javafx.base.jar')
  implementation files ('javafx.controls.jar')
}

application {
  mainModule = 'piglet.friend'
  mainClass = 'piglet.friend.Main'
}

ext.moduleName = 'piglet.friend'

String modulePath = 'C:\\Users\\insec\\Desktop\\javafx-sdk-17.0.1\\lib'

compileJava {
   sourceCompatibility = '17'
   targetCompatibility = '17'
   inputs.property("moduleName", moduleName)
   doFirst {
     options.compilerArgs = [
            '--module-path', modulePath,
            '--add-modules', 'javafx.controls'
     ]
     classpath = files()
   }
}

plugins.withType(JavaPlugin).configureEach {
  java {
    modularity.inferModulePath = true
  }
}
run {
/* inputs.property("moduleName", moduleName)*/
doFirst {
    jvmArgs = [
            '--module-path', modulePath,
            '--add-modules', 'javafx.controls'
              ]
        }
}

jar {
    manifest {
      attributes 'Main-Class': 'piglet.friend.Main'
    }
}


I was able to build a jar file, and was able to run it by specifying the path to the javafx modules at startup. (working code)
java -jar --module-path C:\Users\insec\Desktop\javafx-sdk-17.0.1\lib --add-modules javafx.controls piglet.jar


But there are some problems.
I can't run the program through the run task in gradle. At the output, I get information that the module was not found.
I tried to study what commands the run task executes.

gradle run

C:\Program Files\Java\jdk-17.0.1\bin\java.exe --module-path C:\Users\insec\Desktop\javafx-sdk-17.0.1\lib --add-modules javafx.controls -Dfile.encoding=windows-1251 -Duser.country=RU -Duser.language=ru -Duser.variant --module-path C:\Users\insec\Desktop\piglet\build\libs\piglet.jar --module piglet.friend/piglet.friend.Main

Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: C:\Users\insec\Desktop\piglet\build\libs\piglet.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Package piglet not found in module
:run (Thread[Execution worker for ':',5,main]) completed. Took 0.078 secs.


In general, it does the same thing, the same path to the modules, the same modules.

When developing a project in Intellij, it generally does not see javafx modules (although gradle can build a jar, and I can run it from the console).

I tried adding javafx sdk as a dependency in Intellij. Project Structure -> Libraries ->. It works. But when I update gradle via Intellij, it removes this dependency for some reason and the whole project starts glowing red.

Also when trying to connect a library from a remote repository
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.9'

I can't build jar file anymore with "module not found" error.

My module-info.java file

module piglet.friend{
    requires javafx.controls;
    requires com.google.gson;
    
    opens piglet;
 }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question