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

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

问题描述

我正在尝试使用一组简单的代码行来简单地使用JavaFX,这些代码行是从另一个stackoverflow页面获得的(这里)。但是,问题显然不在于代码,而是在构建和运行过程中有更基本的东西。

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.

这是我的代码:

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

起初我根本无法进行编译。最后我发现我需要在我的javac命令行上放置-classpath c:\Program Files \Oracle \JavaFX 2.1 SDK \lib \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ (这里一个明显的问题是:为什么不在任何显而易见的地方记录(1)这是需要的;(2)如何确切地弄清楚JavaFX安装的路径是什么?!)

At first I couldn't get this to compile at all. Eventually I figured out that I needed to put -classpath c:\Program Files\Oracle\JavaFX 2.1 SDK\lib\rt\jfxrt.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

这是什么意思?看起来它在运行时不知道如何找到类javafx.scene.media.Media。但是,我的%CLASSPATH%变量肯定有c:\Program Files \Oracle \JavaFX 2.1 SDK \lib\rt\jfxrt.jar。

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 Files\Oracle\JavaFX 2.1 SDK\lib\rt\jfxrt.jar" in it.

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

Any ideas? Thank you in advance!

推荐答案

这个问题有点重复手动编译javafx 2.0。

This question somewhat duplicates compile javafx 2.0 manually.

此答案专门针对发布前的JavaFX 2版本Oracle Java 8.对于Oracle JavaFX 8+,JavaFX运行时位于类路径上,因此在编译或运行JavaFX应用程序时无需显式添加它。

Java包括jdk7u6及更高版本(适用于Windows和Linux)和jdk7u4及更高版本(适用于OSX)的JavaFX。

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

下载并使用jdk7u6 +并赢得'需要在任何类路径中指定jfxrt.jar文件,并且所有与JavaFX相关的类路径问题都应该消失。

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.

这是一个指向 jdk7u6的早期二进制构建

对于JavaFX 2.1 on Windows你需要包含jfxrt.j用于编译的类路径中的ar lib(如果使用它的JavaFX平台设置,NetBeans将自动执行此操作)和(如果尚未使用javafxpackager或JavaFX ant任务正确打包应用程序),也可以在运行时。

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.

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

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.

以下是Windows下命令行编译和JavaFX程序执行的示例。

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

启动编辑器:

C:\dev\test>notepad HelloWorld.java

粘贴以下代码并保存:

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();
  }
}

编译并运行 JavaFX 2.2

C:\dev\test>javac -cp "\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld.java
C:\dev\test>java -cp ".;\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld

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

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

C:\dev\test>javac HelloWorld.java
C:\dev\test>java HelloWorld

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

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天全站免登陆