2

I have a JavaFX project that I would want to compile to a jar such that JRE 8 and above are able to run the jar. I am using Intellij IDEA 2017 CE, running on a Mac, using JDK 9.0.1 as my project SDK.

The project compiles fine for the following gradle settings:

sourceCompatibility = 1.9
targetCompatibility = 1.9

However, when I tweak the target compatibility to 1.8:

sourceCompatibility = 1.9
targetCompatibility = 1.8

It shows a stream of errors relating to a missing JavaFX library:

Error:(1, 26) java: package javafx.application does not exist
Error:(2, 23) java: package javafx.geometry does not exist
Error:(3, 23) java: package javafx.geometry does not exist
Error:(4, 20) java: package javafx.scene does not exist
Error:(5, 28) java: package javafx.scene.control does not exist
...

I looked at other stackoverflow answers to see why this is happening such as:

  1. JavaFX comes with JDK 8? Says JavaFX is bundled with JDK 1.8 but I am using JDK 1.9.
  2. JavaFX and OpenJDK Says OpenJDK has JavaFX, but I am not using OpenJDK
  3. cannot resolve symbol javafx.application in IntelliJ Idea IDE Says OpenJDK does not have JavaFX (what?), but I am not using OpenJDK

I have also tried to invalidate the caches before building the project but to no avail.

How do I compile to JRE 8 with my JDK?

Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
  • What are the reasons you think that backward compatibility works this way? You're building in JDK-9 and want to run (or compile) in JRE-8? Why don't you try to write the code in JDK-6 and compile it to JDK-2 binary? I'm afraid that would be *forward* compatibility. – zlakad Mar 04 '18 at 03:34
  • See this link May be help full @Moon cheesez – Ng Sharma Mar 04 '18 at 03:38
  • @zlakad I thought it works this way because after reading https://stackoverflow.com/questions/10663450/whats-the-difference-between-source-and-target-compatibility, it looks like I can tweak the target JDK version to any version I would like. Also, what do you mean that it would be forward compatible? It seems to be as I have a higher version of JDK, supporting a lower version – Moon Cheesez Mar 04 '18 at 03:39
  • https://blog.jetbrains.com/idea/2013/03/packaging-javafx-2-applications-in-intellij-idea-121/ @Moon cheesez – Ng Sharma Mar 04 '18 at 03:39
  • Well, I think if we write some code JDK-9 compatible, we cannot just compile it for JDK-8 because **some** code cannot be applied to the target. I didn't read links you provided, but IMHO **both** source and target **must** be the same. IDK if intelij-idea (or any other IDE at this point) *can* actually deal with this issue. I'd try to write the whole project using the JDK-8 – zlakad Mar 04 '18 at 03:47
  • @AK.Sharma I don't understand the steps in the link provided. I think the guide is for an older version of IntelliJ (The url says 2013). I also didn't configure IntelliJ to use a JavaFX template. – Moon Cheesez Mar 04 '18 at 03:56
  • @zlakad According to [this answer](https://stackoverflow.com/a/10663507/4428725) in the post I linked, it said that "You could use a Java SE 7 compiler and compile to run on Java SE 1.5", so I assume that you can use Java SE 9 compiler and compile to run on Java SE 1.8? – Moon Cheesez Mar 04 '18 at 04:00
  • I read it, but I think those answers approves what I'm thinking. Is it so hard to install/use JDK-8 and set the project properties for source and target to JDK-8 Java platform? This is the very small step... – zlakad Mar 04 '18 at 04:05
  • @zlakad I see what you mean, so you are suggesting that I use JDK 1.8 to compile instead of 1.9. Your previous comments looked like you were telling me to rewrite my code to be compatible with 1.8, which I thought meant that I will have to somehow replace all my JavaFX code in 1.8. As to why those errors show up, I still find it a mystery... Add your answer below so that I can accept it – Moon Cheesez Mar 04 '18 at 04:27
  • My friend, I don't care about the reputation, so I will not convert my comments to answer. There is no mystery - IDE developments are NOT suitable for JDK-9 (no exclusions)! I really think you should rewrite your project using the JDK-8 – zlakad Mar 04 '18 at 04:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166176/discussion-between-moon-cheesez-and-zlakad). – Moon Cheesez Mar 04 '18 at 04:42

0 Answers0