简体   繁体   中英

How to compile a Java source code (using javac 11.0.13) that uses JavaFx on a Linux machine?

The line import javafx.application.Application; gives me the following compile-time error:
error: package javafx.application does not exist

I'm using Linux Lite 5.6 (Ubuntu 20.04) and java --version gives me:

openjdk 11.0.13 2021-10-19 LTS
OpenJDK Runtime Environment Corretto-11.0.13.8.1 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.13.8.1 (build 11.0.13+8-LTS, mixed mode)

I thought JavaFX came together with Java SDK but that doesn't seem to be the case since JDK 11.
In order to compile and run JavaFX apps, the JavaFX SDK (which no longer comes together with the Java SDK) is required.


These are the steps required to follow in order to be able to compile and run JavaFX apps:

Step 1. Checking if you already have Java installed

You can check if you have Java installed by running java --version in the terminal. The output should be something like this:

 openjdk 17.0.1 2021-10-19 OpenJDK Runtime Environment (build 17.0.1+12-39) OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)

If you get an output similar to this (note that you can have a different version installed), then proceed to Step 3.
If you get an error that probably means you don't have Java installed and you need to install it. ( go the next step )

Step 2. Installing/Downloading Java

You can find the Java SDK for downloading here .
Click on JDK 17 .
From Builds choose and download the appropriate version for you system.
Save it somewhere on your system. I will use ~/Programs .

It is recommended that you also check the sum of the file.

For that download the sha256 file next to the Java version you chose and save it where you saved the Java archive.

Open a terminal or use an existing one and cd to the place where you downloaded the Java archive and the.sha256 file.

At the moment I'm writing this the sha256 for Linux/x64 doesn't contain the filename.

You can either edit the .sha256 file and add on the first line, right after the checksum, 2 spaces followed by the name of the Java archive you downloaded
you can run echo " <name/of/java/archive>" | tee --append <name/of/.sha256/file> echo " <name/of/java/archive>" | tee --append <name/of/.sha256/file>
to achieve that

it should like like this 1c0a73cbb863aad579b967316bf17673b8f98a9bb938602a140ba2e5c38f880a openjdk-17.0.1_linux-x64_bin.tar.gz ,

then run
sha256sum -c <name/of/java/archive>
(in my case, the name is openjdk-17.0.1_linux-x64_bin.tar.gz )

or, if you don't want to edit the file, then you can run
test $(sha256sum <name/of/java/archive> | awk '{print $1}') = $(cat <name/of/.sha256/file>) && echo OK

The output in the first method should be <name/of/java/archive>: OK and in the second it should be OK

Extract the archive with the command tar -xvzf <name/of/java/archive>

Go to the directory that was created by extracting the file, in my case it's jdk-17.0.1
and run ./bin/java --version to check if java was downloaded correctly and that it works.

Now you can make the java and javac commands available from anywhere or you can go to Step 3. .

Method 1: aliases

add

alias javac="<path/to/jdk>/bin/javac" alias java="<path/to/jdk>/bin/java"

to ~/.bashrc (or the equivalent, depending on your distro/shell)

Method 2: adding the bin folder to the PATH environment variable

add
export PATH=$PATH:<path/to/jdk>/bin
to ~/.bashrc (or the equivalent, depending on your distro/shell)

Step 3. installing/downloading JavaFX

Go here , head to Downloads section, choose the SDK that fits your system and download it. Eventually check the sha256 sum.

Extract the archive using unzip <archive/name>

For simplicity you can add aliases for compiling and running JavaFX applications. I'm using the following:

 alias java='/home/kamui/Programs/jdk-17.0.1/bin/java' alias javac='/home/kamui/Programs/jdk-17.0.1/bin/javac' alias javafx='java --module-path /home/kamui/Programs/javafx-sdk-17.0.1/lib --add-modules javafx.controls,javafx.fxml' alias javafxc='javac --module-path /home/kamui/Programs/javafx-sdk-17.0.1/lib --add-modules javafx.controls,javafx.fxml'

Note that you don't need to add javafx.fxml if you don't use it

Step 4. compile & run

Single source code file

For a single source code file, like HelloFX.java you can run the following commands:

 # make sure you are in the directory where HelloFX.java is located javafxc HelloFX.java # to compile it javafx HelloFX # to run it

The source for these steps, where you can also find the HelloFX.java file is https://openjfx.io/openjfx-docs/#install-javafx

Multiple source code files

For more complex directory structures, like this one (it's just what IntelliJ provides when you create a new JavaFX Project) you can do the following:

Make sure you are in the folder where src is located.

Compile with:
javafxc -d./out -sourcepath src/ src/main/java/com/example/a7/HelloApplication.java

Run with:
javafx -classpath./out com.example.a7.HelloApplication Notes:

  1. I have no idea what module-info.java is or does.
  2. When I used the CLI to compile the code generated by IntelliJ for a JavaFX project, it failed at runtime (more specifically, at loading the FXML) because it seems that it can only find it inside the out folder (I tested it using getClass().getResource() and getClass().getClassLoader().getResource() )
    In order to fix this you can move/copy the.fxml file somewhere in the out folder and set the proper path for the FXMLLoader inside the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM