神刀安全网

A JavaFX HelloWorld using Java 9’s Project Jigsaw in 60 seconds

By now you’ve probably have heard of Java 9’s new module system a.k.a. project Jigsaw. If you don’t know about Java 9’s new module system, you should visit Mark Reinhold’s paper on The State of the Module System . Also, you should check out @nipafx Nicolai Parlog’s excellent blog at http://blog.codefx.org There he goes into great detail about Java 9’s new module system and many scenarios.

In this article I will attempt to show you how to create a JavaFX Helloworld application using Java 9’s module system in 60 seconds.

Requirements

As of this writing Java 9 is still in the early access phase which means you’ll need to grab the latest builds at https://jdk9.java.net/download/

  • JDK 9 EA build 114 or greater ( JDK 9 EA )

Instructions

Assuming you’ve installed your JDK and have the appropriate environment variables set. Also, you’ll want to get to the command prompt or terminal and type: java -version to verify the installed JDK. Eg.

A JavaFX HelloWorld using Java 9’s Project Jigsaw in 60 seconds

Step 1:   Create a directory in your home directory for your project such as the following for Windows OS, Linux, and MacOS respectively. Also, create a src directory under your helloworld project directory.

# Windows   c:/Users/myusername>md helloworld   c:/Users/myusername>md helloworld/src      # Linux   /user/home/myusername $ mkdir -p helloworld/src    # Mac   /Users/myusername$ mkdir -p helloworld/src   cd helloworld

Step 2:   Create a directory using a simple naming convention based on Java 9’s new way to organize source code and modules.

Create a directory named as your module eg: com.mycompany.helloworld . Assuming your current directory is <User’s Home dir>/helloworld/

mkdir src/com.mycompany.helloworld

Step 3:   Create directories based on your package namespace of the HelloWorld.java file. In this simple example the main Helloworld.java will be created using the following directory structure:

mkdir -p src/com.mycompany.helloworld/com/mycompany/helloworld

The directories should look like the following:

<User Home dir>/               helloworld/                  src/                     com.mycompany.helloworld/                        com/                           mycompany/                              helloworld/

Step 4:     Create a module-info.java file. This lets the compiler know which core module dependencies the application needs to compile and run such as javafx modules. Create a file named module-info.java under the directory helloworld/src/com.mycompany.helloworld.  Copy and paste the code below into the module-info.java file. Use vi, nano or notepad.

module com.mycompany.helloworld {    requires javafx.base;    requires javafx.graphics;    requires javafx.controls;    exports com.mycompany.helloworld; }

Step  5:     Create the HelloWorld.java application file. The file will be created in the directory helloworld/src/com.mycompany.helloworld/com/mycompany/helloworld/ . Copy and paste the code below into the HelloWorld.java application.

package com.mycompany.helloworld;  import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage;  /**  * A JavaFX Hello World  */ public class HelloWorld extends Application {     /**     * @param args the command line arguments     */     public static void main(String[] args) {        Application.launch(args);     }      @Override     public void start(Stage stage) {        stage.setTitle("Hello World");        Group root = new Group();        Scene scene = new Scene(root, 300, 250);        Button btn = new Button();        btn.setLayoutX(100);        btn.setLayoutY(80);        btn.setText("Hello World");        btn.setOnAction( actionEvent ->                   System.out.println("Hello World"));        root.getChildren().add(btn);        stage.setScene(scene);        stage.show();     } }

Step  5:     Compile the source code with the -d option with the location of compiled module. The directory for the new helloworld module is mods/com.mycompany.helloworld .

javac -d mods/com.mycompany.helloworld src/com.mycompany.helloworld/module-info.java src/com.mycompany.helloworld/com/mycompany/helloworld/HelloWorld.java

Step  6:     Execute the Hello World application as a module. After compiling the module into the mods directory you will now use Java 9’s new -modulepath option to specify the compiled module’s directory. Also, you will specify the -m option to execute the module and its main class HelloWorld. Use the following command:

java -modulepath mods -m com.mycompany.helloworld/com.mycompany.helloworld.HelloWorld

The output of the previous command:

A JavaFX HelloWorld using Java 9’s Project Jigsaw in 60 seconds

Conclusion

Not sure if you’ve taken more than 60 seconds, but assuming your environment is setup and the JDK 9 is installed you should be able to cut and paste the code in seconds. I find the new Java 9 module system pretty straight forward. Although there is a little extra typing in order to let the compiler know where modules are, it’s really not that different than the -classpath option.

I can see how large projects can break apart components as modules and benefit from it.  I believe these are truly exciting times in the Java world because tools will be able to build thin executables, thus faster load times. I feel it’s been a very long time coming, but a very needed feature that will encourage us to write, manage and deploy modular software.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » A JavaFX HelloWorld using Java 9’s Project Jigsaw in 60 seconds

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址