0

I'm trying to compile a Java program using the Java 11 compiler from the command line on Arch Linux. The program compiles and works great from Eclipse IDE; however, when I try to compile it using the 'javac' command, it throws the following error:

javac MyApp.java 
MyApp.java:3: error: package javafx.application does not exist
import javafx.application.Application;
                         ^
MyApp.java:4: error: package javafx.stage does not exist
import javafx.stage.Stage;

Any help would be appreciated.

Regards

Al1nuX
  • 393
  • 1
  • 4
  • 17

2 Answers2

3

JavaFX is not part of the Java SDK anymore and needs to be installed separately (or you use Maven, Gradle or another build system). See the official documentation.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • I'm aware of this, however the program compiles perfectly on Eclipse IDE. How can I do the same from the command line. – Al1nuX Oct 01 '21 at 11:10
  • 2
    @Al1nuX you have to make sure to have the relevant JavaFX jar files on the classpath when you compile with `javac`. It would be much easier if you would use a build tool which can automatically manage dependencies for you such as [Maven](http://maven.apache.org/) or [Gradle](https://gradle.org/) instead of trying to compile it manually with `javac`. – Jesper Oct 01 '21 at 11:15
  • Thanks for the reply, how can I make sure to have the relevant JavaFX jar files on the class file? – Al1nuX Oct 01 '21 at 13:33
  • 1
    Actually, some vendors *do* offer a JDK bundled with the OpenJFX (JavaFX) libraries: Azul Systems, and BellSoft. – Basil Bourque Oct 01 '21 at 15:43
  • 1
    @Al1nuX Use the `--class-path` argument or, preferably, the `--module-path` argument if you're using JavaFX 9+. The reason it works in your IDE is because that IDE sets those arguments automatically, based on the dependencies you've added to the project. Build tools do this, too. – Slaw Oct 01 '21 at 21:46
  • I have installed the Liberica JDK package, I'm getting this error: Error: Could not find or load main class MyApp Caused by: java.lang.ClassNotFoundException: MyApp – Al1nuX Oct 03 '21 at 06:55
2

Build tools

Use a build tool such as Maven or Gradle to (a) download the desired version of the OpenJFX (JavaFX) libraries, and (b) include those libraries in your build, to be bundled inside your final JAR file.

JDK bundled with libraries

Or, use a JDK that includes the OpenJFX (JavaFX) libraries. Use such a JDK to compile. And ensure that your users have such a JDK installed on their computer.

This may work well in a controlled setting such as a corporate office, but may not be practical for distributing to the public.

At least two JDK vendors provide a variant of their distributions to include OpenJFX (JavaFX) libraries: Azul Systems, and BellSoft.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • "to be bundled inside your final JAR file." -> I don't think this is a supported configuration. JavaFX should be [run from the module path](https://bugs.openjdk.java.net/browse/JDK-8256422). The JavaFX platform consists of multiple modules. Placing [multiple modules in a single jar file is not supported](https://stackoverflow.com/questions/47875351/multiple-java-9-modules-from-a-single-maven-module). What would be supported would be "to be bundled *with* your final JAR file.". – jewelsea Oct 01 '21 at 21:54
  • Note, [hacks](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing) exist currently to make single jar files which include JavaFX to work. Those hacks are even currently documented at the openjfx.io site, but the JavaFX development team have explicitly stated that they won't support such configurations (see links from previous comment). – jewelsea Oct 01 '21 at 21:59
  • I have installed the Liberica JDK package, I'm getting this error: Error: Could not find or load main class MyApp Caused by: java.lang.ClassNotFoundException: MyApp – Al1nuX Oct 03 '21 at 06:58