0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Mac Eclipse Mavenで作ったJavaFXアプリ(java11)をJREのないWindowsで実行する

Posted at

目的

開発環境はMac Eclipseを使ってMaven使ってるけど、実行環境はjreの入っていないwindows PCという状況で、Windows PCではjavaのダウンロード含め行わない前提でJavaFXアプリを実行させたい。
アプリケーションやjreのバージョンアップのたびにつまづくので、覚書。

参考リンク
JavaFX 8からJavaFX 11以降へのマイグレーション

実行可能jarの生成

dependencyの追加

java11からはJavaFXが含まれていないので、Maven dependencyに必要なJavaFXを追加する。
<classifier>の指定に関してはこれであっているのか、よく分からない。

pom.xml
<dependencies>
  <dependency>
  	<groupId>org.openjfx</groupId>
  	<artifactId>javafx-base</artifactId>
  	<version>11.0.2</version>
  </dependency>
  <dependency>
  	<groupId>org.openjfx</groupId>
  	<artifactId>javafx-controls</artifactId>
  	<version>11.0.2</version>
  	<classifier>win</classifier>
  </dependency>
  <dependency>
  	<groupId>org.openjfx</groupId>
  	<artifactId>javafx-fxml</artifactId>
  	<version>11.0.2</version>
  	<classifier>win</classifier>
  </dependency>
  <dependency>
  	<groupId>org.openjfx</groupId>
  	<artifactId>javafx-web</artifactId>
  	<version>11.0.2</version>
  	<classifier>win</classifier>
  </dependency>
</dependencies>

plug-inの追加

dependenciesも含んだ実行可能jarを生成するためにmaven-assembly-pluginを追加する。

pom.xml
    <plugins>
      <plugin>
      	<artifactId>maven-assembly-plugin</artifactId>
      	<executions>
      		<execution>
      			<id>make-assembly</id>
      			<phase>package</phase>
      			<goals>
      				<goal>single</goal>
      			</goals>
      		</execution>
      	</executions>
      	<configuration>
      		<descriptorRefs>
      			<descriptorRef>jar-with-dependencies</descriptorRef>
      		</descriptorRefs>
      		<archive><manifest><mainClass>XXX.XXX.Main</mainClass></manifest></archive>
      	</configuration>
      </plugin>
    </plugins>

実行可能jarの生成

maven:packageでtargetディレクトリにxxx-jar-with-dependencies.jarが出来上がる。

windows用カスタムjreの作成

配布用なので、なるべく容量の小さいjreを作成するため、jlinkを使ったカスタムjreを作成する。

windows用jdkの入手

image.png
oracle java製品のページからwindows用sdkを入手する。zipの方で。保存先は任意。

windows用JavaFxの入手

Gluonページからwindows用のjavafx(jmods)を入手する。保存先は任意。x64かx86かは実行する先のPCで確認。(コントロールパネルの詳細から確認可能)

JavaFXのバージョンと対応するjdkバージョンに注意。
jdk11対応のLTSならJavaFx17。
image.png

カスタムjreの作成

jlinkを使ってカスタムjreを作成する。

zsh
% jlink --module-path jdk-11.0.20/jmods/:javafx-jmods-17.0.8/ --add-modules javafx.controls,javafx.fxml --output jre-win

先ほど入手したjdk-11.0.20/jmods/でwindows用のjdkを指定している。これを指定しなかった場合は、$JAVA_HOME/jmodsが使用されるので、Mac用のjdkが使用されてしまうはず。
複数のmodule-pathを指定するときは;ではなく:で区切る。ちょっとつまづいた。

-pまたは--module-path modulepath
モジュール・パスを指定します。
このオプションが指定されていない場合、デフォルトのモジュール・パスは\$JAVA_HOME/jmodsとなります。 このディレクトリには、java.baseモジュールとその他の標準モジュールおよびJDKモジュールが格納されています。 このオプションが指定されていてもjava.baseモジュールを解決できない場合、jlinkコマンドは$JAVA_HOME/jmodsをモジュール・パスに追加します。1

windows側での実行

windowsではbatファイルのダブルクリックで実行できるようにしたい。

ファイルの配置

今回は簡単のため、カスタムjre、実行可能jar、batファイル共にデスクトップに配置した。

pathの追加

javaを使うためにコマンドパスを通す。

コマンドプロンプト
path C:¥Users¥%USERNAME%¥Desktop¥jre-win¥bin;%PATH%

batファイルの完成

launch.bat
cd C:¥Users¥%USERNAME%¥Desktop
path C:¥Users¥%USERNAME%¥Desktop¥jre-win¥bin;%PATH%
java -jar 実行可能jar名.jar
  1. https://docs.oracle.com/javase/jp/13/docs/specs/man/jlink.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?