JavaFX - Passing Arguments


I received a query on how to pass arguments in JavaFX. It basically depends on how we deploy the application. The application can be deployed using JavaWebStart, as an applet or launched from command line.

Lets take a simple application..

var text = Text {
    x: 10, y: 30
    font : Font size : 16 }
    content: "Argument {FX.getArgument( "key" )}"
}

The argument is passed as key-value pair. The value is retrieved using FX.getArgument( <key> ) method. Arguments can be passed to various deployment modes as shown below.

Command Line:

javafx -cp FXArguments.jar fxarguments.Main key="From Command Line"


Applet:

<script src="http://dl.javafx.com/1.2/dtfx.js"></script>
<script>
    javafx(
        {
              archive: "FXArguments.jar",
              draggable: true,
              width: 250,
              height: 80,
              code: "fxarguments.Main",
              name: "FXArguments"
        } , {
              key: "from Applet"
        }
    );
</script>

Launch Applet Example


Java Web Start:

<jnlp spec="1.0+" codebase="dist" href="FXArguments.jnlp">
    <application-desc main-class="com.sun.javafx.runtime.main.Main">
        <argument>MainJavaFXScript=fxarguments.Main</argument>
        <argument>key=from Java Web Start</argument>
    </application-desc>
</jnlp>

Launch Web Start Example

Source:

<script type="text/javascript">var dzone_url = "http://blogs.sun.com/rakeshmenonp/entry/javafx_passing_arguments";</script> <script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>

Comments:

Is it also possible to pass in an argument as a URL parameter for a web start application?

Like this:
www.test.com/app.jnlp?key=arg1&test=other

/Pär

Posted by Pär Dahlberg on June 13, 2009 at 08:26 AM IST #

@Pär Dahlberg That wont be possible. One way is to write a servlet with content-type as XML which will dynamically generate the jnlp file. This way we can pass the arguments to servlet which returns the required jnlp file.

Posted by Rakesh Menon on June 14, 2009 at 03:58 AM IST #

Hadn't thought of that. Thanks :)

/Pär

Posted by Pär Dahlberg on June 22, 2009 at 06:42 PM IST #

i'm just looking for this, thanks a lot Rakesh!

Posted by LaoZhang on June 25, 2009 at 01:36 AM IST #

@LaoZhang Glad! Thanks!

Posted by Rakesh Menon on June 25, 2009 at 04:48 AM IST #

How do I pass jvm arguments to the applet? I'm running up against a problem with localization and cant' figure how to translate this

-Duser.language=fr -Duser.country=CA

in to something that can be used by the applet. I've tried adding

<param name="java_arguments" value="-Duser.language=fr -Duser.country=CA"/>

inside the <applet-desc> tag inside the jnlp file, but no luck.

thanks

Posted by Sean C on October 26, 2009 at 04:55 PM IST #

@Sean C You can use property tag inside resources in jnlp to set System properties.

<property name="key" value="overwritten"/>

There are some security restrictions for unsigned applets.

Please refer to jnlp syntax for more info : http://java.sun.com/javase/6/docs/technotes/guides/javaws/developersguide/syntax.html#resources

Posted by Rakesh Menon on October 27, 2009 at 05:30 AM IST #

i am using a webappication that calls a fx script. i would like to pass an object form a java class and fetch the values into the fx script from this object. can it be done if yes then please help.

Posted by sim on April 18, 2010 at 03:20 AM IST #

@sim I'm not clear on requirement. If its java class, can it be integrated as part of JavaFX application? If java class is in different application, will it be possible to lookup and get remote instance?

If not, other alternative is to pass information as XML or pass the attributes of the object as a series of name=value pair.

Posted by Rakesh Menon on April 19, 2010 at 02:09 AM IST #

Hi,
How do you integrate JavaFX with Java. I have a JavaFX player which takes as argument the path of a local file. I want to pass this from a Java Program called action which takes arguments from a form bean in a package called action. This player is in a directory called javafxplayer. I want to pass the string in action to string in javafx.
Also, I want to know how the JavaFX jar file is generated. The action class actually returns a JSP page which contains the JavaFX player embedded. So, can I paste the default html code generated by IDE for the player into this. And will the jar file in this jsp file be updated. (The jar file is generated by default in the IDE for the first time that the JavaFX player is called.)
Also, could you please explain in general how the jar file would be generated for the first time?

Posted by Balaj on April 27, 2010 at 12:15 AM IST #

@Balaji You can build the javafx jar using NetBeans or javafxpackager command. Then you can copy the jnlp & jar files and host it in your webserver. Edit the codebase in jnlp to point to actual hosted URL. Then copy-paste the content of html file and put it in your jsp file. You can pass additional arguments as mentioned in above post.

You may also refer to
http://blogs.sun.com/rakeshmenonp/entry/javafx_jsp

Posted by Rakesh Menon on April 27, 2010 at 02:06 AM IST #

Hi,
Thanks for your tips in the previous post. I actually followed them. But, my application is still not working. Here is a brief description of my project and things I have done so far. Inside web-inf/src, i have a package VIEW where all my jsp pages are stored. Among them is a page player.jsp that actually display the player that is embedded. There is a package PLAYER where I have my javafx script for a player. I copied the main.jnlp,main_browser.jnlp and Main.jar to the VIEW folder. I modified the codebases of both the jnlp. I also copied the main.html script to my player.jsp. I am getting the attribute from request and setting an attribute "key" which is the file to be played. The source of file to be played by the player is obtained using FX.getargument. Besides this, I also installed and included jnlp-servlet.jar in lib in my web app. Besides I also modified web.xml with the following:
<servlet>
<servlet-name>JnlpDownloadServlet</servlet-name>
<servlet-class>com.sun.javaws.servlet.JnlpDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JnlpDownloadServlet</servlet-name>
<url-pattern>\*.jnlp</url-pattern>
</servlet-mapping>

The following error is returned:
exception: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct..
java.io.FileNotFoundException: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.
at sun.plugin2.applet.JNLP2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.io.FileNotFoundException: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.
exception: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct..
java.io.FileNotFoundException: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.
at sun.plugin2.applet.JNLP2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.io.FileNotFoundException: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.
exception: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct..
java.io.FileNotFoundException: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.
at sun.plugin2.applet.JNLP2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.io.FileNotFoundException: JNLP file error: Main_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.

Please let me know what could be the problem and any other steps I need to take to accomplish. I would also like to mention that the argument to the player may change in every request.

TIA

Posted by Balaji on April 29, 2010 at 04:24 AM IST #

@Balaji Can you check the value of "codebase" in jnlp? It may be pointing to different URL. I don't think you need to install jnlp-servlet.jar, its required only if you are using various versions for applications.

Posted by Rakesh Menon on April 29, 2010 at 06:48 AM IST #

Hi Rakesh,
I copied the jnlp from the dist folder in which the jnlp and jar files are generated by default by Eclipse in Project2/dist/ to Project/web-inf/src/view/. I updated codebase in the jnlp to point to Project/web-inf/src/view/. This the directory where they have been shifted to. Should we give the absolute path or the relative path.
Also, should we move the lib files generated in dist. to view directory. To reiterate, the player.jsp is also in the view directory.

regards.
Balaji

Posted by Balaji on April 29, 2010 at 02:34 PM IST #

@Balaji Since these jars and jnlps are to be accessed from client side (browser), it needs to be proper http url (If you type that url in browser, you must be able to download the jar or launch jnlp). Its not file path, its http url. Btw, I'm not sure if web-inf is the right place, it should be outside web-inf, so that it can be directly accessed from browser.

Posted by Rakesh Menon on April 30, 2010 at 02:10 AM IST #

Post a Comment:
Comments are closed for this entry.
About


The views expressed on this blog are my own and do not necessarily reflect that of my organization

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today