使用 JavaFX 2.1 编译和运行 [英] Compiling and running with JavaFX 2.1

查看:85
本文介绍了使用 JavaFX 2.1 编译和运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从另一个 stackoverflow 页面(此处).但是,问题显然不在于代码,而在于构建和运行过程中的一些更基本的东西.

这是我的代码:

import javafx.scene.media.Media;导入 javafx.scene.media.MediaPlayer;...Media medMsg = new Media("msg.mp3");媒体播放器 medplMsg = 新媒体播放器(medMsg);medplMsg.play();

起初我根本无法编译.最终我发现我需要将 -classpath c:Program FilesOracleJavaFX 2.1 SDKlib tjfxrt.jar 放在我的 javac 命令行上.(这里一个明显的复杂问题是:为什么没有在任何明显的地方记录它 (1) 需要这样做以及 (2) 如何准确地确定 JavaFX 安装的路径是什么?!)

但是,当我运行代码时,我得到:

线程main"中的异常 java.lang.NoClassDefFoundError: javafx/scene/media/Media在 progtest.main(progtest.java:120)引起:java.lang.ClassNotFoundException:javafx.scene.media.Media在 java.net.URLClassLoader$1.run(Unknown Source)在 java.net.URLClassLoader$1.run(Unknown Source)在 java.security.AccessController.doPrivileged(Native Method)在 java.net.URLClassLoader.findClass(Unknown Source)在 java.lang.ClassLoader.loadClass(来源不明)在 sun.misc.Launcher$AppClassLoader.loadClass(来源不明)在 java.lang.ClassLoader.loadClass(来源不明)... 1个

这是什么意思?看起来它在运行时不知道如何找到类 javafx.scene.media.Media.但是,我的 %CLASSPATH% 变量中肯定有c:Program FilesOracleJavaFX 2.1 SDKlib tjfxrt.jar".

有什么想法吗?提前谢谢你!

解决方案

这个问题有点重复compilejavafx 2.0 手动.

这个答案专门针对Oracle Java 8 发布之前的JavaFX 2 版本.对于Oracle JavaFX 8+,JavaFX 运行时在classpath 上,所以编译或JavaFX 时不需要显式添加运行应用程序.

Java 在 jdk7u6 及更高版本(适用于 Windows 和 Linux)和 jdk7u4 及更高版本(适用于 OSX)中包含 JavaFX.

下载并使用 jdk7u6+,您无需在任何类路径中指定 jfxrt.jar 文件,所有与 JavaFX 相关的类路径问题都会消失.

这里是 jdk7u6 早期二进制构建的链接.

对于 Windows 上的 JavaFX 2.1,您确实需要在类路径中包含 jfxrt.jar 库以进行编译(如果您使用它的 JavaFX 平台设置,NetBeans 将自动执行此操作)和(如果您没有使用javafxpackager 或 JavaFX ant 任务),也在运行时.

用于 Linux 的 JavaFX 2.1 是预发行版(以防您使用它).对于 Linux 预发行版,如果 Linux 上的 JavaFX 运行时未正确设置,则只需在编译和运行时在类路径中包含 jfxrt.jar.

这里是一个windows下JavaFX程序的命令行编译和执行示例.

启动编辑器:

C:dev	est>notepad HelloWorld.java

粘贴以下代码并保存:

import javafx.application.Application;导入 javafx.scene.Scene;导入 javafx.scene.control.Label;导入 javafx.stage.Stage;公共类 HelloWorld 扩展应用程序 {公共静态无效主(字符串 [] args){发射(参数);}@覆盖公共无效开始(阶段阶段){stage.setScene(new Scene(new Label("Hello World")));舞台表演();}}

编译并运行它JavaFX 2.2:

C:dev	est>javac -cp "Program FilesOracleJavaFX 2.2 Runtimelibjfxrt.jar" HelloWorld.javaC:dev	est>java -cp ".;Program FilesOracleJavaFX 2.2 Runtimelibjfxrt.jar" HelloWorld

对于 Oracle Java 8+,不需要显式的 JavaFX 类路径说明符:

C:dev	est>javac HelloWorld.javaC:dev	est>java HelloWorld

请注意,通常您不仅要编译代码并运行它,还要使用 javafxpackager 或 javafx ant 任务打包代码.这些任务会将启动器类嵌入到您的打包应用程序中,该类将检测 JavaFX 运行时的版本和位置,因此您无需指定 jfxrt.jar 位置,除非您想覆盖平台的默认位置.

I am trying a very simple use of JavaFX using a simple set of lines of code which I got from another stackoverflow page (here). But, the problem is clearly not with that code but with something more fundamental in the build and run process.

Here is my code:

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
.
.
.
Media medMsg = new Media("msg.mp3");
MediaPlayer medplMsg = new MediaPlayer(medMsg);
medplMsg.play();

At first I couldn't get this to compile at all. Eventually I figured out that I needed to put -classpath c:Program FilesOracleJavaFX 2.1 SDKlib tjfxrt.jar on my javac command line. (One obvious complex of questions here is: Why isn't it documented in any obvious place (1) that this is needed and (2) how exactly to figure out what the path to the JavaFX installation is?!)

But, when I run the code I get:

Exception in thread "main" java.lang.NoClassDefFoundError: javafx/scene/media/Media

    at progtest.main(progtest.java:120)
Caused by: java.lang.ClassNotFoundException: javafx.scene.media.Media
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

What does this mean? It looks like it doesn't know, at runtime, how to find the class javafx.scene.media.Media. But, my %CLASSPATH% variable definitely has "c:Program FilesOracleJavaFX 2.1 SDKlib tjfxrt.jar" in it.

Any ideas? Thank you in advance!

解决方案

This question somewhat duplicates compile javafx 2.0 manually.

This answer is specifically for JavaFX 2 versions before the release of Oracle Java 8. For Oracle JavaFX 8+, the JavaFX runtime is on the classpath, so you don't need to explicitly add it when compiling or JavaFX running applications.

Java includes JavaFX in jdk7u6 and above (for Windows and Linux) and jdk7u4 and above (for OSX).

Download and use jdk7u6+ and you won't need to specify the jfxrt.jar file in any classpath and all of your JavaFX related classpath issues should go away.

Here is a link to an early binary build of jdk7u6.

For JavaFX 2.1 on Windows you do need to include the jfxrt.jar lib in your classpath for compile (NetBeans will do this automatically if you use it's JavaFX platform settings) and (if you haven't packaged your app correctly using the javafxpackager or JavaFX ant tasks), also at runtime.

JavaFX 2.1 for Linux is a pre-release (in case you are using that). For the Linux pre-release you would only have to include jfxrt.jar in your classpath at both compile and runtime if the JavaFX runtime on Linux was not set up correctly.

Here is an example of a command line compilation and execution of a JavaFX program under windows.

Launch an editor:

C:dev	est>notepad HelloWorld.java

Paste the following code and save it:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class HelloWorld extends Application {
  public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage stage) {
    stage.setScene(new Scene(new Label("Hello World")));
    stage.show();
  }
}

Compile and run it JavaFX 2.2:

C:dev	est>javac -cp "Program FilesOracleJavaFX 2.2 Runtimelibjfxrt.jar" HelloWorld.java
C:dev	est>java -cp ".;Program FilesOracleJavaFX 2.2 Runtimelibjfxrt.jar" HelloWorld

For Oracle Java 8+, the explicit JavaFX classpath specifier is not required:

C:dev	est>javac HelloWorld.java
C:dev	est>java HelloWorld

Note that usually rather than just compiling the code and running it, you would also package the code with javafxpackager or the javafx ant tasks. These tasks will embed a launcher class into your packaged app which will detect the version and location of the JavaFX runtime so that you don't need to specify the jfxrt.jar location unless you want to override the default location for the platform.

这篇关于使用 JavaFX 2.1 编译和运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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