Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get javafx classes into runtime?

Tags:

maven

javafx

I am willing to try javafx for the first time, I went to Oracle javafx site to find that javafx now comes with the JDK 7u25. I updated my java version but I am not sure what I am looking for. I get "cannot find symbol" errors whenever I try to import and use a javafx class. I found "javafx-mx.jar" "javafx-doclet.jar" "ant-javafx.jar" files in %JAVA_HOME%/lib but as far as I understand, those are meant to be used by the JRE not the JDK. I would really like not to be dependant on any eclipse plugins. I use maven for the build process. I have seen a couple of javafx maven plugins, but they seem to target the javafx tools. Am I getting the wrong picture? How would I get those classes to be imported correctly?

EDIT

Now I am able to compile the project using what I found here. It suggest to add:

<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>javafx</artifactId>
  <version>2.2</version>
  <scope>system</scope>
  <systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency>

This solved my build issues, but I can't run the program, I am trying to run this simple example, but I get this error:

java.lang.NoClassDefFoundError: javafx/application/Application
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Any ideas on how to add the same library to runtime? I am using maven-exec-plugin.

like image 438
amaurs Avatar asked Oct 04 '22 12:10

amaurs


1 Answers

Just making jfxrt.jar a maven system dependency is not recommended

I don't think integrating maven with your JavaFX build via a system dependency to jfxrt.jar is a good idea.

Using that method you will be able to build your application (because the system dependency for the javafx runtime classes will allow maven to find them).

But when you go to run your application with Java 7, you will still need to manually add the classpath to your java.exe command, similar to Compile code using JavaFX 2.0 (using command line).

I don't use the maven exec plugin, but see add a jar to maven exec:java classpath, which provides instructions on setting the appropriate classpath for that exec plugin, if you want to use the tool in this way.

Preferred approach

The preferred approach is to properly package your application using a recommended deployment tool (e.g. JavaFX ant tasks, javafxpackager, javafx-maven-plugin or javafx-gradle-plugin).

These tools will embed a small wrapper main class in your application jar which will find the JavaFX runtime if it is available or help guide users through a runtime install if it is not.

Also, if you use Oracle Java 8, javafx will be on your default classpath so you won't need to worry about classpath related issues.

like image 86
jewelsea Avatar answered Oct 12 '22 11:10

jewelsea