LoginSignup
3
3

More than 5 years have passed since last update.

なぜJavaFXアプリはmain()メソッド無しで動くのか

Last updated at Posted at 2015-01-22

JavaFXでウィンドウを表示するための最低限のコード」という記事の中で、JavaFXアプリケーションにはmain()メソッドは不要であると書きました。その点について少し調べてみました。

Oracleの「JavaFX: Getting Started with JavaFX」というドキュメントの中に、次のような記述を発見しました。

The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.

(http://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm)

自分なりに訳してみると、

JavaFXアプリのJARファイルがJavaFXパッケージャ・ツールを使って作成された場合、 JARファイル内にJavaFXランチャーが埋め込まれるので、main()メソッドは必要ありません。 しかし、JavaFXランチャーを持たないJARファイル(JavaFXツールを完全に統合していないIDEで作成されたもの)も起動させられるように、main()メソッドを記述しておくことは有用です。また、JavaFXコードを埋め込んだSwingアプリケーションにもmain()メソッドが必要です。

要は

  • JavaFX Packager toolがキッチリ働いてくれればランチャーが埋め込まれるからmain()メソッドは必要ない
  • でも、そうでない環境もあるから念のため書いておいた方が良い

ってことになるかと思います。なので、「最低限」を目指すわけではない通常のコーディングでは記述するようにします。
ちなみに、JavaFX Packager toolについてはこちらに詳しい説明がありました。

3
3
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
3
3