JavaFX – Set JAVAFX_HOME Path on Ubuntu

javajavafxjdkjreopenjdk

So my problem is openjfx and using it on Scala-sbt project. The actual problem is JAVAFX_HOME path.

First of all I have java version 10.0.2 installed on my computer and openjfx (which I take as JavaFX) is on ubuntu repositories based on java 8jre (openjdk-8-jre is required). So when I install it needs another version of java.

But still after installing openjfx my sbt build will not open a project and gives an error

java.lang.ExceptionInInitializerError

Caused by: java.util.NoSuchElementException: key not found: JAVAFX_HOME

I take that this since java current version is 10 and javafx is on 8. However when I try to do

 update-alternatives --config java

and select version 8, java seems not to work on at all.
The output of java –version says then:

Unrecognized option: –version

Error: Could not create the Java Virtual Machine.

Error: A fatal exception has occurred. Program will exit.

So,
Is there a way to export only javafx to .bashrc similar to this:

export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"

to get JAVAFX_HOME path working or do I have to reinstall java to java 8 somehow to get javafx working correctly?

Best Answer

Ok so sbt had errors since the build file had references to the system environemnt [system.getenv("JAVA_HOME")) in java and in sbt it was scala.sys.env("JAVA_HOME") and similarly to JAVAFX_HOME]

It is common to use JAVA_HOME system variable so this is added in linux by adding the location of java installation to the /etc/environment file. Mine was JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64". Remember to not use CLASSPATH in the environment file since it hinder the running of java files.

/etc/environment:

...
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
JAVAFX_HOME="/usr/share/java/openjfx"  

Remember to reboot after setting new environment variables.

Also note that update-alternatives should be done both to java and javac. And the

java --version 

does not work since in java 8 it is just

java -version

Also in sbt you can use this line to to add

 unmanagedJars in Compile += Attributed.blank(
   file(System.getenv("JAVA_HOME") + "/jre/lib/jfxrt.jar"))

to set current jfxrt (javaFX) file wherever that is (modify accordingly)