10
10

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 5 years have passed since last update.

Ubuntu18.4でjava11とjavaFxの環境を整える

Last updated at Posted at 2018-11-18

環境

ubuntu

___@kubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.1 LTS
Release:	18.04
Codename:	bionic

javaを入れたらこうなる

___@kubuntu:~$ java --version
opensdk 11.0.1 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

___@kubuntu:~$ javac --version
javac 11.0.1

openjdkをインストールする

2018/11/18現在、aptでopenjdk-11-jdkをインストールするとopenjdk10がインストールされる
今後切り替えるらしい

今回はsdkmanを使ってjava11をインストールする

  1. https://sdkman.io/install の通りにやるだけでsdkmanをインストールできる
  2. https://sdkman.io/sdks の通りにやるだけでjava11をインストールできる
  3. java --versionとjavac --versionで確認する

sdkmanでインストールした場合のjavaの本体は

/home/ユーザー名/.sdkman/candidates/java/11.0.1-open

にある

Intellijなどでjdkを指定するときはここのパスを書く

javaFxをダウンロードする

  1. https://openjfx.io/ からダウンロードする

今回は/optに配置することにした

/opt/openjfx-11.0.1_linux-x64_bin-sdk/
└── javafx-sdk-11.0.1
    ├── legal
    ...略
    └── lib
    ...略

intellijで動かす

javafxのプロジェクトを作る

作り方は他人のを参考に

今回はgradleとかを使わない設定

  1. 適当にコードを書く。どこかのサンプルでもいいね
    intelij_sample.png

  2. 左上の[Main▼]を押してedit configurationsを開く

  3. jdkとかの設定を書く
    editconfig.png

  4. VM Optionは下記のように設定する
    vmoption.png

--module-path=/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib
--add-modules=javafx.controls
--add-modules=javafx.swing
--add-modules=javafx.base
--add-modules=javafx.fxml
--add-modules=javafx.media
--add-modules=javafx.web
  1. OKを押す
  2. Shift+F10で実行する

ハマりどころ

ネットで普通に検索するとjavafx.controlsだけ入れてる記事が多く、それでは不完全で、
その状態だと以下のような例外が出るのでモジュールは全部入れること
cannotexecute.png

/home/___/.sdkman/candidates/java/11.0.1-open/bin/java -Djava.library.path=/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib --module-path=/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib --add-modules=javafx.controls -javaagent:/home/___/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/182.5107.16/lib/idea_rt.jar=43339:/home/___/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/182.5107.16/bin -Dfile.encoding=UTF-8 -classpath /home/___/sandbox/javafxtry/out/production/javafxtry:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/src.zip:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx-swt.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.web.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.base.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.fxml.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.media.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.swing.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.controls.jar:/opt/openjfx-11.0.1_linux-x64_bin-sdk/javafx-sdk-11.0.1/lib/javafx.graphics.jar sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x2f837822) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x2f837822
	at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
	at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
	at sample.Main.start(Main.java:13)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
	at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
	at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
	at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
	... 1 more
Exception running application sample.Main

Process finished with exit code 1
10
10
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
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?