2

I have just started a new project based on AdoptOpenJDK 11 and OpenJavaFX 11.

Before during my development, I used VM Arguments which was not always practical when you pass the project to someone else.

So I started to replace my VM Arguments with the module-info.java file.

VM Arguments before:

--module-path ${PATH_TO_FX} –add-modules javafx.controls,javafx.fxml 

--add-opens
javafx.base/com.sun.javafx.runtime=ALL-UNNAMED
--add-opens
javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED
--add-opens
javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED
--add-opens
javafx.base/com.sun.javafx.binding=ALL-UNNAMED
--add-opens
javafx.base/com.sun.javafx.event=ALL-UNNAMED
--add-opens
javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED
--add-opens
javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED

module-info.java file now:

module hellofx {

requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;

opens org.openjfx to javafx.fxml;
exports org.openjfx;

}

This is where I have a problem, I managed to replace the tag --module path but I can not find a solution for tags --add-open.

Can someone help me?

Edit 12/09:

Now I have a new error with this two lines in the module-info.java

Duplicate requires entry: javafx.fxml

How I can solve this?

ValentinDP
  • 323
  • 5
  • 14
  • Does this answer your question? [How do Java Module directives impact reflection access into a module?](https://stackoverflow.com/questions/53927375/how-do-java-module-directives-impact-reflection-access-into-a-module) – howlger Dec 06 '19 at 14:18
  • I suppose “I managed to replace the tag `--module path`” actually means “I managed to replace `–add-modules`” and that’s all you can achieve. You can not declare in your module that others open towards you. That would defeat the entire modularization. The fix is to rewrite your application to not rely on interna of other modules. – Holger Dec 06 '19 at 15:28
  • Perhaps my answer [on defining module info for a JavaFX project](https://stackoverflow.com/a/57897944/1155209) might help you. – jewelsea Dec 06 '19 at 17:26
  • You should open *your* application’s root package to ‘javafx.graphics,javafx.fxml;‘ not ‘org.openjfx’. – jewelsea Dec 06 '19 at 17:31
  • If you are still stuck, when you run the modularized app, you will get a stack trace, Edit the question to add it. – jewelsea Dec 06 '19 at 17:35
  • @jewelsea I have a new error now, check my edits. Thanks – ValentinDP Dec 09 '19 at 14:50
  • The error message seems obvious, you have a duplicate `requires` entry for `javafx.fxml` in your `module-info.java` file, so you need to remove one of the duplicates. I don't know what you're module-info.java file is, my guess is that it is different than whatever you have in your question. – jewelsea Dec 09 '19 at 18:24
  • @jewelsea No it's exactly the same I wrote, I could put a picture to show it. By cons in JavaFX, JAR files also contain modules files that could come from there. – ValentinDP Dec 10 '19 at 12:51

1 Answers1

2

Option A Quick and clean :

Change


module hellofx {

requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;

//wrong
opens org.openjfx to javafx.fxml;

//right
opens com.your.package to javafx.fxml, javafx.controls;

exports org.openjfx;

}

Option B (Because building a Jar with javaFX seems to be wierd)

You also can do this with Gradle (because you then can easily build your JavaFX jar without much of a Problem)

You should add the gradle fx Plugin, and define Compile arguments.

When you use Gradle you also don't need to download the openJavaFX library manually.

Here is how your Gradle file should look like




buildscript {
    dependencies {
        classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.7.0'
        classpath 'org.openjfx:javafx:11'

    }
    repositories {
        mavenLocal()
        mavenCentral()
         maven {
            url "https://plugins.gradle.org/m2/"
        }

    }
}

plugins {
    id 'eclipse'
    id 'java'
    id 'java-library'
    id 'application'
    // This is very Important!
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.jlink' version '2.12.0'
}

sourceCompatibility = 11
targetCompatibility = 11
repositories {
    jcenter()
    mavenCentral()
}



mainClassName="com.your.MainLauncher"



// Use this for the Runtime, to run with the required Modules

javafx {
    version = "11"
    modules = [ 'javafx.controls', 'javafx.fxml','javafx.graphics']
}


sourceSets {
    main.java.srcDir "src/main/java"
    main.resources.srcDir "src/main/resources"
}

// Your dependecies
dependencies {

    compile group: 'org.openjfx', name: 'javafx', version: '11.0.2'
    // JAVA FX dependecy. Important!
    // Add your other dependecies


}

// to build the jar
jar {
  manifest {
    attributes(
      'Main-Class': 'com.your.MainLauncher'
    )

  }
  from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

// to "include compile arguments " that replace your VM arguments
compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls,javafx.fxml,javafx.graphics'
        ]
        println options.compilerArgs
    }

}


you can run this with gradle but also with eclipse. When you build the jar there are also no VM arguments required. I Hope this will help you.

This does also run out of your IDE(intelliJ / Eclipse) if you have Gradle Support plugins installed.

Luxusproblem
  • 1,913
  • 11
  • 23
  • Hi, to use this, you need an internet connection? – ValentinDP Dec 09 '19 at 13:13
  • @ValentinDubois-Pivot Yes you need internet, at least when you refresh your project or for the initial load of the Dependencies. Once downloaded Via Gradle there is no need for internet, except you want to update your dependencies. – Luxusproblem Dec 10 '19 at 14:14
  • This computer has no way to connect to the internet. It is dedicated to a rescritive use. So it can not work with this method? – ValentinDP Dec 10 '19 at 14:23
  • @ValentinDubois-Pivot It can work, but in that case, you need to have the required Dependencies, already downloaded or stored somwhere: `implementation files('libs/your_jar.jar')` . You the PC you develop on has no internet or the Machine the code should be deployed has no internet ? – Luxusproblem Dec 10 '19 at 14:37
  • The Pc has no connection so I can download and store the required dependencies. – ValentinDP Dec 10 '19 at 14:41