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

3
  • try upgrading your JDK. Eclipse uses its own compiler Commented Oct 1, 2021 at 10:49
  • What version of Java are you using, and what Linux distro / version? Commented Oct 1, 2021 at 10:51
  • Java 11 and JavaFX 11 on Arch Linux. Commented Oct 1, 2021 at 11:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm aware of this, however the program compiles perfectly on Eclipse IDE. How can I do the same from the command line.
@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 or Gradle instead of trying to compile it manually with javac.
Thanks for the reply, how can I make sure to have the relevant JavaFX jar files on the class file?
Actually, some vendors do offer a JDK bundled with the OpenJFX (JavaFX) libraries: Azul Systems, and BellSoft.
@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.
|
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.

3 Comments

"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. The JavaFX platform consists of multiple modules. Placing multiple modules in a single jar file is not supported. What would be supported would be "to be bundled with your final JAR file.".
Note, hacks 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).
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.