2

Working on creating an application with JavaFX and the Gradle build tool:

  • JavaFX: 17.0.1
  • Gradle: 7.3.1
  • Java: Amazon Coretto 17.0.3
  • JUnit: 5.8.2

I encountered the following error while running unit tests in Junit

> Task :test FAILED
Error occurred during initialization of boot layer
java.lang.RuntimeException: Unable to parse --add-opens <module>/<package>: com.example.testingtesting/

Execution failed for task ':test'.
> Process 'Gradle Test Executor 2' finished with non-zero exit value 1

I have tried increasing my heap size and cloning the project to another location. I have also gone through the gradle docs and the gradle github and have not been able to find anything of particular use.

In addition I have also gone through the IntelliJ setup and configuration tutorials and the JavaFX tutorials but I cannot seem to find anything on the structure or dependencies or anything else relating to the unit tests. I have included additional code information below. (not including the full example because I found the problem)

Directory structure is as follows:

├───.gradle
└───src
    ├───main
    │   └───java
    │       └───com
    │           └───example
    │               └───testingtesting
    └───test
        └───java
            └───!!JUnitTestingFile!!

Gradle Build Script

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.10'
    id 'org.beryx.jlink' version '2.24.1'
}

group 'com.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.8.2'
}

sourceCompatibility = '17'
targetCompatibility = '17'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'com.example.testingtesting'
    mainClass = 'com.example.testingtesting.HelloApplication'
}

javafx {
    version = '17.0.1'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {

    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}

jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'app'
    }
}

jlinkZip {
    group = 'distribution'
}

1 Answers1

3

Turns out that I missed something in the Directory structure as I found out with this wonderful tutorial and its corresponding git repo.

The problem for me was in the directory structure, which should have been the following:

├───.gradle
└───src
    ├───main
    │   └───java
    │       └───com
    │           └───example
    │               └───testingtesting
    └───test
        └───java
            └───com
                └───junit5
                    └───!!JUnitTestingFile!!

(note the addition of the tests.java.com.junit5 folder instead of just the tests.java folder)

I will not claim to fully understand why the directory structure caused an error, so if anyone has any more knowledge as to why this happens please share.

I also checked that the given gradle build file continues to work with the new build structure despite having a slightly different dependency from the example project and it does.

  • 1
    `src/test/java` is the "root" of the test-source path. Anything directly under `src/test/java` is not in a package. The Java Platform Module System does not allow classes to be in the default/unnamed package. But that's sort of tangential here. The real problem is this error: `Unable to parse --add-opens /: com.example.testingtesting/`. Looks like a plugin tried to add an `--add-opens` argument. You can see the format is `/`. But the value was `com.example.testingtesting/`. No package. Thus, adding `com/junit5` fixes the issue, because now... [cont.] – Slaw May 24 '22 at 03:19
  • 1
    [cont.]... the test class is in the `com.junit5` package. Now the JVM argument would be `--add-opens com.example.testingtesting/com.junit5=`. – Slaw May 24 '22 at 03:22