Friday, August 9, 2019

Installing Java, Eclipse and JavaFX


Note that in these instructions, something in angle brackets, such as <javadirectory>, is a placeholder for a value that can vary.

Installing Java

There are variations on implementation of a Java Development Kit (JDK).  Even more numerous are variations on Java Virtual Machine (JVM) implementation.  However, the main two to consider here are Oracle JDK and OpenJDK.  With respect to functionality, they are currently pretty much the same:

"From Java 11 forward, therefore, Oracle JDK builds and OpenJDK builds will be essentially identical." - https://blogs.oracle.com/java-platform-group/oracle-jdk-releases-for-java-11-and-later

Getting the Oracle JDK involves having or creating a free account at Oracle and logging in before downloading.  Getting the OpenJDK has less friction so that is recommended.  https://openjdk.java.net/ will point to https://jdk.java.net/12/ to get version 12.  Previous versions will work, but might as well get the most recent stable version as it seems that the support cycle is now shorter than before.  Oracle does have their Version 11 as a Long Term Support (LTS) version, but that seems to be only for a commercial license. 

1—Download the OpenJDK.  Downloading from the above location is a .zip file rather than an installer.

2—Unpack it and move it to the desired location, such as a directory under “C:\Program Files”. 

3—Add the location <javadirectory>/bin (this is probably something like C:\Program Files\Java\jdk-12.0.1\bin , depending upon where you chose to put it) to the system Path variable.  Access the Path variable in Windows 10 through: Settings -> System -> About -> System Info -> Advanced System Settings , Environment Variables button.

4--To test that it is installed and that it can be seen correctly, open a Windows command line window.  This can be done either through a Start menu item (Windows System -> Command Prompt) or the use of the Search Box in the Task Bar (type “cmd” or “command prompt” or something similar).  At the Command Prompt, type “java –version” to establish that is a JVM visible from everywhere.  Type “javac –version” to establish that there are JDK tools visible from everywhere.  (You can use this test to determine the level of Java support installed on an unfamiliar system.  Various Microsoft Windows systems you encounter may have none, only a JVM or both a JVM and JDK installed.)

 Installing Eclipse

Download the Eclipse installer at https://www.eclipse.org/ .  Normally you install applications in the “C:\Program Files” (or “C:\Program Files (x86)” for 32-bit applications) directory.  Depending upon which Windows 10 release you have and how permissions are set up on your machine, the installer may complain about trying to install in “C:\Program Files”.  It will likely try and default to a directory under “C:\Users\<currentuser>” which should work fine.

Installing JavaFX

JavaFX is intended to replace Swing as the standard GUI library for Java SE (Standard Edition).  Since the JDK 11 release in 2018, JavaFX is part of the open-source OpenJDK, under the OpenJFX project.  Back in version 8, it was part of the Oracle JDK download.  Now it is downloaded and installed separately. 

The main site for JavaFX is here: https://openjfx.io/ .  The download link on this page directs to https://gluonhq.com/products/javafx/ .  Gluon is a development tools, libraries and infrastructure company that would appear to be hosting the JavaFX SDK, among other things.  JavaFX can be downloaded in two forms from this location: as a platform-specific SDK and as a set of JMODs.  JMODs can be used at compile and link time, but not at run time.  (https://stackoverflow.com/questions/51843208/running-javafx-sample-on-jdk-11-with-openjfx-11-jmods-on-module-path)  So download the SDK form.  As to which version, it would be a debate between the most recent version and the LTS (Long Term Support) version.  While it seems that the Long Term Support may only be available via contract, download that version anyway.  The support fixes may end up being publicly available.  As of this writing, the public LTS version is 11.0.2 .  (By contrast, the most current version is 12.0.2 .)

Instructions on what to do upon downloading are here: https://openjfx.io/openjfx-docs/ (the Getting Started link on the openjfx.io page).  Jump to the “Run HelloWorld using JavaFX 12” section (from the column of links on the left side).  Unzip the JavaFX SDK downloaded .zip file and copy the JavaFX sdk directory to an appropriate permanent location if it is not already there.  Next to the Java SDK directory would make sense, such as “C:\Program Files\Java\javafx-sdk-11.0.2” .  On the instructions web page, click on the Windows tab to get the Windows version of the commands.  The instructions would suggest using the ‘set’ command at the Command Prompt to create the environment variable “PATH_TO_FX” .  The problem with this method is that the newly created environment variable will only remain for the duration of that particular command line session.  For Windows, a more permanent way would be to create the new variable in the same place that the PATH variable was updated, namely Settings -> System -> About -> System Info -> Advanced System Settings -> Environment Variables .  Use New… to create the System Variable, with PATH_TO_FX being the Variable name and the directory path being the Variable value.  (To show all of the current environment variables at the Windows command prompt, use the ‘set’ command with no parameters.)

Testing the FX installation:  There is a link to the HelloFX.java sample program from the above openjfx-docs page.  This goes to a GitHub repository.  We will skip over pulling it down with Git until later, for now it is easy enough to copy the program off the web page and paste it into a text browser.  Save the program as HelloFX.java to a working directory.  There is a command on the documentation page to compile:

  javac --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX.java 

If you have the PATH_TO_FX variable something like C:\Program Files\Java\javafx-sdk-11.0.2\lib , this command will result in an three line error message:

error: invalid flag: Files\Java\javafx-sdk-11.0.2\lib
Usage: javac <options> <source files>

use --help for a list of possible options

This is because there is a space in the pathname.  This can be rectified by adding quotes around the part of the command that decomposes the PATH_TO_FX variable:

javac --module-path "%PATH_TO_FX%" --add-modules javafx.controls HelloFX.java

This should create a class file in the working directory.  Run it using the java command, again adding the quotes in the command:

java --module-path "%PATH_TO_FX%" --add-modules javafx.controls HelloFX

This should pop up a new window on the screen with the text “Hello, JavaFX 11.0.2, running on Java 12.0.1” in the middle (if all of the installed versions are the same).
 


1 comment:

  1. Do you know how to solve the error
    javac: invalid flag: --module-path

    from javac --module-path "%PATH_TO_FX%" --add-modules javafx.controls Shapes.java?

    ReplyDelete