如何将javafx类转换为运行时? [英] How can I get javafx classes into runtime?

查看:153
本文介绍了如何将javafx类转换为运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿意第一次尝试使用javafx,我去 Oracle javafx网站查找javafx现在配备了JDK 7u25。我更新了我的java版本,但我不确定我在寻找什么。每当我尝试导入和使用javafx类时,我都会收到找不到符号的错误。我在%JAVA_HOME%/ lib中找到了javafx-mx.jarjavafx-doclet.jarant-javafx.jar文件,但据我了解,这些文件是由JRE而不是JDK使用的。我真的不想依赖任何eclipse插件。我使用maven进行构建过程。我见过几个javafx 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?

编辑

现在我能够编译项目使用我在这里找到的内容。它建议添加:

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)

关于如何将相同的库添加到运行时的任何想法?我正在使用 maven-exec-plugin

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

推荐答案

建议不要让jfxrt.jar成为maven系统依赖

我不认为通过系统依赖到jfxrt.jar将maven与JavaFX构建集成是一个好主意。

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

使用该方法,您将能够构建应用程序(因为javafx运行时类的系统依赖性将允许maven找到它们)。

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).

但是当您使用Java 7运行应用程序时,仍然需要手动将类路径添加到java.exe命令,类似于使用JavaFX 2.0编译代码(使用命令行)

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).

我不使用maven exec插件,但请参阅向maven exec:java classpath 添加一个jar,它提供了有关为该exec插件设置适当类路径的说明,如果你想以这种方式使用该工具。

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.

首选方法

首选方法是使用推荐的部署工具正确打包应用程序(例如 JavaFX ant任务 javafxpackager javafx-maven-plugin javafx-gradle-plugin )。

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).

这些工具会在您的应用程序jar中嵌入一个小的包装器主类,如果它可用,它将找到JavaFX运行时,或者如果不可用,则帮助引导用户完成运行时安装。

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.

此外,如果您使用 Oracle Java 8 ,javafx将位于您的默认类路径中,因此您不需要担心与类路径相关的问题。

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.

这篇关于如何将javafx类转换为运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆