12

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

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!

Community
  • 1
  • 1
  • I think you have something wrong with your setup. It shouldn't be necessary to put any JavaFX jars manually on the classpath. How are you building and running your JavaFX application? With the ant scripts provided by NetBeans? – Puce Jun 04 '12 at 21:14
  • No, I am not using NetBeans but working directly from the console. My javac line is: 'javac -classpath "c:\Program Files\Oracle\JavaFX 2.1 SDK\rt\lib\jfxrt.jar";..\bin -d ..\bin ..\src\progtext.java' –  Jun 04 '12 at 21:55

2 Answers2

9

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\test>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\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

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

C:\dev\test>javac HelloWorld.java
C:\dev\test>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.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thank you so much jewelsea. This is very helpful. Before I try installing jdk7u6, I would like to see what is necessary to get my code to run with jdk7u4, which I already have. How do I get my program to run as things are now? Do you have any ideas? –  Jun 04 '12 at 21:53
  • Small correction: Even for JavaFX 2.1 pre-release for Linux, jfxrt.jar is not needed on the classpath if setup correctly. – Puce Jun 04 '12 at 22:16
  • Thanks Puce, I updated the answer to include your correction. – jewelsea Jun 04 '12 at 22:21
  • I tried what you suggest above and got exactly the same error, even when I ran the VM with the -cp parameter you suggested (you changed it slightly from the one I had been using). Then I installed jdk 7u6 from the link you supplied, jewelsea. Now I get "java.lang.IllegalArgumentException: uri.getScheme() == null! at com.sun.media.jfxmedia.locator.Locator.(Unknown Source)" which looks like it can't find the file I am supplying the name of, which is there, right in the directory I am calling java from, and in which the class I am running resides. –  Jun 04 '12 at 22:58
  • And, since my previous post in this subthread, I built the HelloWorld application you sent, jewelsea. It worked fine, as I expected! Now, the problem is, what is going wrong with what I am doing above? –  Jun 04 '12 at 23:03
  • And, when I added the lines of code that I am trying to run to the end of the HelloWorld application, I got the same error as in my test application. Hmmm. Looks like the problem is in the advice from the other thread. –  Jun 04 '12 at 23:08
  • To fetch your media based on the classpath: new Media(getClass().getResource("msg.mp3").toExternalForm()); – jewelsea Jun 05 '12 at 02:11
  • Wow, that worked! How in the world did you figure that out? Can you explain this? You're great!!! Thank you!!! But, now the application doesn't end! I have to Ctrl-C to stop it. –  Jun 05 '12 at 02:49
  • Yes, there seems something really screwy going on here. I will try to narrow it down and post another question relating to this. –  Jun 05 '12 at 17:17
  • 1
    The external form lookup for resource idea came from the [JavaFX deployment guide, section 4.3.1](http://docs.oracle.com/javafx/2/deployment/packaging.htm#BABDBHAJ). – jewelsea Jun 05 '12 at 18:41
  • 1
    I created http://javafx-jira.kenai.com/browse/RT-22071 "new Media(source) should locate relative resources similar to new Image(url)", so that your new Media("msg.mp3") request would work as you expected. – jewelsea Jun 05 '12 at 18:54
-4

For Java 7 update 21, Windows XP SP3, I used

javac -cp "\Program Files\Java\jre7\lib\jfxrt.jar"   HelloWorld.java

and

java -cp ".;\Program Files\Java\jre7\lib\jfxrt.jar"   HelloWorld