This discussion is archived
6 Replies Latest reply: Dec 8, 2011 4:31 PM by Jeff Martin RSS

Command-line/Runtime.exec() JavaFX app?

Jeff Martin Newbie
Currently Being Moderated
What is the best way to command-line or Runtime.exec() a JavaFX application on Windows, short of having to find the JavaFX directory and do something like this:

prompt> java -cp c:\Program Files\Oracle\JavaFX 2.0 Runtime\lib\jfxrt.jar my.app.Main

Just doing a normal exec (with runtime installed on the system) results in ClassNotFound.

jeff
  • 1. Re: Command-line/Runtime.exec() JavaFX app?
    jsmith Guru
    Currently Being Moderated
    Package the Java application as a Jar File use the javafxpackager or javafx ant tasks or netbeans.

    After the application is correctly packaged as <app>.jar, run it by either:
    1. double clicking on the jar file in Explorer OR
    2. by typing java -jar <app>.jar from the command line.

    There is no need to put the jfxrt.jar on the classpath - something in the packaging and javafx install allows the jar to find the runtime if the runtime is installed.

    The manifest of the properly packaged jar file will be something like:
    Manifest-Version: 1.0
    JavaFX-Version: 2.0
    implementation-vendor: John_Smith
    implementation-title: JavaFXApplication4
    implementation-version: 1.0
    JavaFX-Application-Class: org.acme.explosives.HelloCss
    JavaFX-Class-Path:
    JavaFX-Fallback-Class: com.javafx.main.NoJavaFXFallback
    Created-By: JavaFX Packager
    Main-Class: com/javafx/main/Main
    The above is generated by the packaging scripts - you don't need to create it manually, I just provided it for reference in case you need to troubleshoot.

    If you want to include your own lib files in your packaging, (and the packaging is done correctly) the manifest will include relative references to the lib files and you just put your lib files in the correct directories relative to your <app>.jar file - note that you don't need to put jfxrt.jar in a relative lib directory as the relative lib file lookup is only for other jars your app relies on.

    Here is a script to package the application from the commmand line using javafxpackager Re: How to compile JavaFX 2.0 app with javac.exe from command prompt? but doing it via netbeans or ant is preferable, zonski also put together something to package via maven if you prefer to use that tool.

    Edited by: jsmith on Dec 8, 2011 12:21 PM
    Added info on relative lib directories.
  • 2. Re: Command-line/Runtime.exec() JavaFX app?
    Jeff Martin Newbie
    Currently Being Moderated
    My situation is a bit more complicated. I've written a Java IDE that runs custom Java code in a player by downloading class files from Google App Engine and dynamically loading them. I have options to kick off the process as a JNLP applet or app, but for the purposes of development and debugging, I need to Runtime.exec() the player.

    I hadn't considered it before, but perhaps there's a "javafx.home" property (or some other way to discern it from the runtime)?
  • 3. Re: Command-line/Runtime.exec() JavaFX app?
    jsmith Guru
    Currently Being Moderated
    but perhaps there's a "javafx.home" property (or some other way to discern it from the runtime)?
    If you could get hold of the source code for com.javafx.main.Main, you might be able to work this out pretty easily, I haven't checked to see if it is in the open source repository yet or not.
  • 4. Re: Command-line/Runtime.exec() JavaFX app?
    817264 Journeyer
    Currently Being Moderated
    prompt> java -cp c:\Program Files\Oracle\JavaFX 2.0 Runtime\lib\jfxrt.jar my.app.Main
    you do not have your application classes on the class path here, do not you?

    spawning an embedded launcher class instead of application main is preferable but if you are confident runtime is available and you know all the paths then i'd assume you should be able to run it this way (with my.app.Main having main() method defined too).

    You can also have your player to create new URLClassLoader, add javafx and application class path to it, create new thread group with this class loader as context class loader, and load your application class from this class loader and run its main. Then you can run your javafx code in the same process (if desired).
    This is pretty much what embedded launcher does anyways (besides checking JavaFX and JRE are installed and handling corner cases).
  • 5. Re: Command-line/Runtime.exec() JavaFX app?
    jsmith Guru
    Currently Being Moderated
    Hi Igor, thanks for your info, how does the embedded launcher know what the javafx classpath is (i.e. the path to the jfxrt.jar)?
  • 6. Re: Command-line/Runtime.exec() JavaFX app?
    Jeff Martin Newbie
    Currently Being Moderated
    Well, it occurred to me that I could simply find the jar URL with:

    URL jfxJarURL = Thread.currentThread().getContextClassLoader().getResource("javafx/scene/Scene.class");

    Then figure out the jar file path from the URL. That seems to work fine.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points