本文介绍了使用JavaFX的Maven项目(在`lib`中使用jar文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个maven项目,即将使用JavaFX。由于我听说JavaFX没有附带所有版本的Java,我下载并将 jfxrt.jar 文件放在 lib 目录。

I'm setting up a maven project, that is going to use JavaFX. Since I've heard JavaFX does not come with all versions of Java, I downloaded and put the jfxrt.jar file in a lib directory in my project.

1)如何指定不应下载依赖项(即JavaFX),但它位于 lib

1) How do I specify that the dependency (ie., JavaFX) should not be downloaded, but which is located in lib?

2)这是否意味着项目可以在任何带JDK的机器上构建(而不是必需的JDK 1.7 - 更新9+)?

2) Does this then mean the project can be built on any machine with JDK (and not necessary JDK 1.7 - update 9+) ?

推荐答案

推荐方法

使用Maven构建JavaFX应用程序:

For building JavaFX apps with Maven:


  1. 使用和/或

  2. 使用Java 8不要为JavaFX指定任何类型的Maven依赖项。

  1. Use a maven JavaFX plugin AND/OR
  2. Use Java 8 don't specify any kind of Maven dependency for JavaFX.

与Java 7不同,使用Java 8时,不必将JavaFX设置为maven依赖项,因为JavaFX位于标准Java运行时类路径上(类似于Swing的方式今天)。

Unlike Java 7, with Java 8 it is unnecessary to set JavaFX as a maven dependency, because JavaFX is on the standard Java runtime classpath (similar to the way Swing is today).

意见

我认为你的配售问题的方法项目 lib 目录中的 jfxrt.jar 文件存在缺陷。

I think the approach in your question of placing the jfxrt.jar file in a project lib directory is flawed.

它有缺陷的原因是:


  1. JavaFX还包括本机库,所以你也需要包含它们(在 jfxrt.jar 可以找到它们的位置。

  2. 给定版本的JavaFX只会被证明可以对抗随附的JDK版本,因此放置在项目的 lib 目录中的JavaFX运行时可能无法用于以后的JDK版本,例如JDK 8。

  3. JDK的未来版本(例如JDK 8)将在JDK的默认类路径中包含JavaFX,因此您可能会与放在 lib中的版本冲突目录。

  1. JavaFX also includes native libraries, so you would need to include them as well (in a location where jfxrt.jar would be able to find them).
  2. A given version of JavaFX will only be certified to operate against the version of the JDK it is shipped with, so the JavaFX runtime you place in your project's lib directory may not work against a later JDK version, such as JDK 8.
  3. Future versions of the JDK such as JDK 8 will include JavaFX on the default classpath of the JDK, so you may get conflicts with the version you place in your lib directory.

总之,JavaFX应该被视为Java运行时系统的一部分,而不是作为项目库。

In summary, JavaFX should be treated as part of the Java runtime system, not as a project library.

JavaFX系统依赖性示例

如果必须使用Java 7并且你不想使用JavaFX maven插件,那么你可以采用不太推荐的方法:

If you must use Java 7 and you don't want to use the JavaFX maven plugin, then you can take the less recommended approach:


  • 引用JavaFX库从jdk(而不是你的项目 lib 目录)作为。

  • Reference the JavaFX library from the jdk (not your project lib directory) as a maven system dependency.

此示例是为Java 7提供的对于Java 8,只需要(并且不会按原样)工作.Java 8,将jfxrt.jar放在$ {java.home} /lib/ext/jfxrt.jar中,它位于默认的Java运行时类路径中,使Java 8的系统依赖性无关紧要。

This sample is provided for Java 7 only and is not required (and will not work as is) for Java 8. Java 8, places the jfxrt.jar in ${java.home}/lib/ext/jfxrt.jar, which is on the default Java runtime classpath, making a system dependency for Java 8 irrelevant.

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

如果使用这样的系统依赖关系打包应用程序,那么您需要编写一些执行在Java 7上运行应用程序时,用户可以将jfxrt.jar添加到运行时类路径中。这是不使用此方法的一个很好的理由。

If you package your application using a system dependency like this, then you need will need to write some execution instructions for your users so that they can add jfxrt.jar to the runtime classpath when running your application on Java 7. This is a very good reason not to use this approach.

如果您选择使用maven系统依赖方法,您可能还希望使用maven 嵌入调用。您可能还想使用来确保您的应用程序是至少针对包含应用程序所需的JavaFX版本的Java所需的最低版本构建。

If you choose to use the maven system dependency approach, you may also wish to use the maven antrun plugin to embed calls to the JavaFX ant tasks as in this sample. You may also want to use the maven enforcer plugin to ensure that your application is built against at least the minimum required version of Java containing a JavaFX version required by your application to work.

这篇关于使用JavaFX的Maven项目(在`lib`中使用jar文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:17