Search code examples
javamavenlaunch4j

Launch4J not recognizing Eclipse Temurin OpenJDK Java 17


I'm using Windows 10 and I have Java 17 installed, with JAVA_HOME set correctly. java --version gives this:

openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment Temurin-17.0.2+8 (build 17.0.2+8)
OpenJDK 64-Bit Server VM Temurin-17.0.2+8 (build 17.0.2+8, mixed mode, sharing)

In a Maven parent POM I have Launch4j via the launch4j-maven-plugin 2.1.1 configured to generate an EXE using my current JRE:

<plugin>
  <groupId>com.akathist.maven.plugins.launch4j</groupId>
  <artifactId>launch4j-maven-plugin</artifactId>
  <version>2.1.1</version>
  <executions>
    <execution>
      <id>generate-exe</id>
      <phase>package</phase>
      <goals>
        <goal>launch4j</goal>
      </goals>
      <configuration>
      ...
      <jre>
        <minVersion>${maven.compiler.release}</minVersion>
      </jre>
...

This parent POM also specifies Java 8 (although it requires Java 9+ to build):

<properties>
  <maven.compiler.release>8</maven.compiler.release>
...

My CLI project POM uses the parent POM above. I build the Maven project with no problem, and it generates my-cli.exe. I can run my-cli.exe just fine.

But I want to use Java 17 in the My CLI, which should be fine since I have Java 17 installed. So I set the following in the My ClI project:

<properties>
  <maven.compiler.release>17</maven.compiler.release>
...

I clean and rebuild the project, and it outputs my-cli.exe just as before. But when I try to run my-cli.exe, it prints:

My CLI: This application requires a Java Runtime Environment 17

Then it opens my browser to https://java.com/en/download/.

From Launch4J Bug 197, it appears that Launch4J doesn't look at the JAVA_HOME environment variable, but instead looks at the Windows registry. That means it ignores any manually-installed JDKs. 😞 So I went to Adoptium and used the installer to install the latest release, OpenJDK17U-jdk_x64_windows_hotspot_17.0.2_8.msi. In my Windows installed programs, it shows "Eclipse Temurin JDK with Hotspot 17.0.2+8 (x64). Unfortunately the same thing happens; Launch4J still doesn't recognize it.

Why is Launch4J not recognizing that OpenJDK 17.0.2+8 is a "Java Runtime Environment 17"?


Solution

  • Launch4j documentation for the <path> element describes the solution:

    ... it is possible to use the JAVA_HOME or PATH environment variables to search for a runtime that was installed without adding registry keys used for searching JREs.

    So just add path element to your launch4j-maven-plugin configuration:

    <plugin>
      <groupId>com.akathist.maven.plugins.launch4j</groupId>
      <artifactId>launch4j-maven-plugin</artifactId>
      <version>2.1.2</version>
        <executions>
          <execution>
            ...
            <configuration>
              ...
              <jre>
                <path>%JAVA_HOME%;%PATH%</path>