93

I tried to create a JavaFX application in IntelliJ Idea IDE but I got compile error that said:

java: package javafx.application does not exist.

I have changed the Project SDK and the Project Language Level to Java 8, reloaded the project but it didn't help. Then I checked if JavaFX plugin was enabled in the settings.

The search on Google and StackOverflow didn't give me more ideas of what is wrong. Thank for any help in advance.

P.S. I am using IntelliJ Idea 14.0 with java8.1.0_25 on archlinux OS.

Holt
  • 36,600
  • 7
  • 92
  • 139
rob111
  • 1,249
  • 1
  • 13
  • 18

13 Answers13

104

As indicated here, JavaFX is no longer included in openjdk.

So check, if you have <Java SDK root>/jre/lib/ext/jfxrt.jar on your classpath under Project Structure -> SDKs -> 1.x -> Classpath? If not, that could be why. Try adding it and see if that fixes your issue, e.g. on Ubuntu, install then openjfx package with sudo apt-get install openjfx.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
Ryan J
  • 8,275
  • 3
  • 25
  • 28
  • I don't have this file in Classpath and in SDK root folder. I don't know why my java that I installed from AUR doesn't have jfxrt.jar file. – rob111 Nov 28 '14 at 01:10
  • My advice would be to go to Oracle and get the proper distribution for your OS. – Ryan J Nov 28 '14 at 05:25
  • 3
    I have added jfxrt.jar file to approipriate directory and added to SDKs. Now my IDE recognize javafx BUT I got runtime Exception Graphics Device initialization failed for : es2, sw Error initializing QuantumRenderer: no suitable pipeline found java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found what do I have to now? – rob111 Nov 29 '14 at 03:06
  • Unfortunately it sounds like you have a system configuration problem... I'm not sure what you can do to fix that as I don't know how custom your particular distribution of Linux is. – Ryan J Nov 29 '14 at 03:56
22

This should be your perfect solution. Try and enjoy. If some command does not work properly that means if you get any error, try to solve it yourself. I have given you the main thing you need. If your application is in a different location, or your system architecture is different, solve it yourself. It's very easy to do it. Just follow my given solution.

Step 0:

sudo apt-get install openjdk-8-jre

Step: 1

sudo apt-get install openjfx

Step 2:

sudo cp /usr/share/java/openjfx/jre/lib/ext/* /usr/lib/jvm/java-1.8.0-openjdk-amd64/lib

Step 3:

sudo cp /usr/share/java/openjfx/lib/* /usr/lib/jvm/java-1.8.0-openjdk-amd64/lib

Step 4:

sudo chmod 777 -R /usr/lib/jvm/java-1.8.0-openjdk-amd64

now open a new project or rebuild your project. Good luck.

Kallac
  • 17
  • 5
Himel Rana
  • 666
  • 5
  • 14
  • 1
    OP is using Arch Linux which does not have APT – xdevs23 Mar 28 '18 at 20:47
  • 4
    `sudo chmod 777 -R /usr/lib/jvm/java-1.8.0-openjdk-amd64` is a bad idea! Don't do it! – Kalle Richter Nov 19 '18 at 20:22
  • Karl Richter. As you are a developer. For development purpose you can do this. Giving 777 permission is bad for business and general user. As you are a developer it should not make any problem. You know well about your OS. >> Also if you want you can do batter by researching more about this problem. Thank you. – Himel Rana Nov 20 '18 at 23:48
  • 9
    Knowing about your OS is an ironic statement when you just changed permissions on your system in a way that allows rootkits and viruses to rewrite the file freely. It doesn't matter what you _think_ you know. NEVER do a chmod 777 on system files. The proper permissions for this folder are 644. A simple `ls -lha` will show that. – Routhinator Mar 01 '19 at 14:06
16

You can use the one that comes with IntelliJ: <intellij>/jre64/lib/ext/jfxrt.jar.

Rok Strniša
  • 6,781
  • 6
  • 41
  • 53
14

You need to download the java-openjfx package from the official Arch Linux repos. (Also, make sure you have the openjdk8-openjdk package). After doing that, open your project in Intellij and go to Project-Structure -> SDKs -> 1.8 -> Classpath and try removing the old JDK you had and clicking on the directory for the new JDK that will now contain jfxrt.jar.

patterkyle
  • 141
  • 2
  • 3
  • 7
    This tells him exactly what he needs to do to make Intellij find the package it's currently not finding. How is that not answering his question? – patterkyle Jan 25 '16 at 23:57
3

Another way to resolve the problem : Click the left mouse's button on the project folder in the project structure at the left, and click on "open module settings" in the list of actions In the new windows, click on SDKs which is under the menu title "Platform settngs" Then check on the "Classpath" list if you can find the jfxrt plugin path, if not, click on the + symbol on the right and select the repertory of the jfxrt plugin (C:\Program Files\Java\jdk1.8.0\jre\lib\ext\jfxrt.jar on my desktop)

3

Sample Java application:

I'm crossposting my answer from another question here since it is related and also seems to solve the problem in the question.

Here is my example project with OpenJDK 12, JavaFX 12 and Gradle 5.4

  • Opens a JavaFX window with the title "Hello World!"
  • Able to build a working runnable distribution zip file (Windows to be tested)
  • Able to open and run in IntelliJ without additional configuration
  • Able to run from the command line

I hope somebody finds the Github project useful.

Instructions for the Scala case:

Additionally below are instructions that work with the Gradle Scala plugin, but don't seem work with Java. I'm leaving this here in case somebody else is also using Scala, Gradle and JavaFX.

1) As mentioned in the question, the JavaFX Gradle plugin needs to be set up. Open JavaFX has detailed documentation on this

2) Additionally you need the the JavaFX SDK for your platform unzipped somewhere. NOTE: Be sure to scroll down to Latest releases section where JavaFX 12 is (LTS 11 is first for some reason.)

3) Then, in IntelliJ you go to the File -> Project Structure -> Libraries, hit the ➕-button and add the lib folder from the unzipped JavaFX SDK.

For longer instructions with screenshots, check out the excellent Open JavaFX docs for IntelliJ I can't get a deep link working, so select JavaFX and IntelliJ and then Modular from IDE from the docs nav. Then scroll down to step 3. Create a library. Consider checking the other steps too if you are having trouble.

It is difficult to say if this is exactly the same situation as in the original question, but it looked similar enough that I landed here, so I'm adding my experience here to help others.

Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
  • Best solution ever. Saving me days and hours of searching to just run a small "Hello world" for javafx. – Bohao LI Jan 28 '22 at 19:30
3

In IntelliJ Idea,

Check the following things are configured properly,

Step 1:

File -> Setting -> Plugins -> search javafx and make sure its enabled.

Step 2: Project Structure (Ctrl+Shift+Alt+s)

Platform Settings -> SDKs -> 1.8 -> Make sure Classpath should have "jre\lib\ext\jfxrt.jar"

Step 3:

Project Settings -> Project -> Project SDK - should be selected 1.8

Project Settings -> Project -> Project language level - configured as 8

Ubuntu: If not found jfxrt.jar in your SDKs then install sudo apt-get install openjfx

Stone
  • 583
  • 6
  • 8
2

You might have a lower project language level than your JDK.

Check if: "Projeckt structure/project/Project-> language level" is lower than your JDK. I had the same problem with JDK 9 and the language level was per default set to 6.

I set the Project Language Level to 9 and everything worked fine after that.

You might have the same issue.

Raphael S
  • 41
  • 3
2

If you are working with IntelliJ and JDK 11

1. go to project structure ctrl + alt + shift + s.

2. add the mising javafx libraries in the Global Libraries section.

enter image description here

3. add a module-info.java file with the required dependencies to your module.

module modulename {
    requires javafx.fxml;
    requires javafx.controls;
    opens sample;
}

4. rebuild the project !

5. enjoy !

Saif Faidi
  • 509
  • 4
  • 15
  • Is a `modulename` a root package? What if I don't have a root package (all packages are inside src folder) – parsecer May 18 '23 at 15:41
0

I had the same problem, in my case i resolved it by:

1) going to File-->Project Structure---->Global libraries 2) looking for jfxrt.jar included as default in the jdk1.8.0_241\lib (after installing it) 3)click on + on top left to add new global library and i specified the path of my jdk1.8.0_241 Ex :(C:\Program Files\Java\jdk1.8.0_241).

I hope this will help you

Djak aim
  • 1
  • 2
0

I am using apache maven with java 11 and I was able to remove all java fx problems by adding this dependency to my pom.xml file:

    <!-- JAVA FX -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>17.0.1</version>
    </dependency>
Stimpson Cat
  • 1,444
  • 19
  • 44
0

For me the simplest solution was to use Amazon Corretto, it is pretty much same as Oracle java.

Follow the steps:

  1. Go to Intellij, File -> Project Structure
  2. Go to SDK
  3. Download SDK
  4. From the dropdown, choose 1.8 as Java 1.8 version comes bundled with JavaFX library.

And you are good to go.

Aakash Parashar
  • 386
  • 1
  • 2
  • 14
0

I had a similar issue, my error message was slightly different though:

java: package javafx.event does not exist

I found one solution to fix this issue. Instead of adding dependencies, modifying CLASSPATH or trying different JDK / SDK versions, you can simply select:

File -> New Project -> JavaFX -> Location of the code

After restarting, the build is successful.

Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29