14 Replies Latest reply: Jan 24, 2012 1:40 AM by jsmith RSS

    How to deploy Javafx 2.0 Application in web server and accsess by URL

    905754
      Hello All,

      I have built an application of JavaFx 2.0 using Netbeans 7.1. It has created one html file, one jar file one jnlp file with the name of Project. Now I want to deploy my application into some server e.g. glassfish or tomcat so that other user can access it through URL.

      Is it possible to deploy the application onto server. If yes then how?

      I have found one link
      http://docs.oracle.com/javafx/2.0/deployment/deploy_overview.htm
      here it have described various executions mode. In one execution mode it state that we can Launch application from a remote server using Web Start. But I am not able to get steps how to achieve this.


      Thanks,
      Vikas
        • 1. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
          jsmith
          Just place a copy of the jar, jnlp and html file output by the NetBeans build into a directory of your webserver, then access the html file in your browser.

          For example, let's say you called your application MyPowerApp and netbeans output MyPowerApp.html and you wanted to deploy that to a local Tomcat server.
          1. Download and install a copy of Tomcat (http://tomcat.apache.org/download-70.cgi)
          2. Copy the jar, jnlp and html file into tomcat's webapps/ROOT directory.
          3. Start tomcat.
          4. Access your application via http://localhost:8080/MyPowerApp.html
          The applet will start automatically and you can click on the link to launch the application via WebStart.

          To update the application just rebuild it in NetBeans, copy it over into the tomcat webapps/ROOT directory and refresh your browser link (no need to restart Tomcat).

          In practice you will want to modify the html rather than use the generated html to embed either the applet or WebStart link somewhere on your website (you won't need both execution modes in a single page like the Netbeans generated sample).
          • 2. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
            905754
            Hi jsmith,

            Thanks for your quick reply.

            I am having one config file and some images. when I am running it through jar or jnlp its reading the config file and displaying the images but when I am trying to access the through URL after copying into tomcat\webapp\root folder (as you suggested) it is not able to read the config file and images.
            I think it is path related issue.
            Where should I keep my images folder and config file folder?

            I tried to copy it into same tomcate\webapp\root folder but it is not working.

            Thanks in advance.
            Vikas

            Edited by: Vikas Agrawal on Jan 17, 2012 4:29 AM
            • 3. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
              sumani
              I have tried this and the application is accessible from broswer. Thanks for that.

              But I hit a problem.

              My resource files (images, properties, config files) are not loading so application unable to work as expected.

              What care should be taken so that the application can recognize the files?
              • 4. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                jsmith
                I place a static method on my the class which extends Application:
                // get a resource relative to the application class.
                static String getResource(String path) {
                  return MyApp.class.getResource(path).toExternalForm();
                }
                And then I place all the resources in the same package as the Application class and have the build system bundle them in the jar with the Application.
                Then whenever I need a resource I just call the static getResource method with just the filename of the resource.

                If you want to place your resources in a relative directory to the Application class, then you can call the getResource method with a relative path with no "/" at the start of the path.
                Or if you want resources from the root of the jar prefix the resource name with "/".

                Edited by: jsmith on Jan 17, 2012 3:48 AM

                If you do things this way and are still having issues, unpack the jar and make sure your resources are really in your jar, if not you will need to play around with the copy tasks in your build system to get them in the jar.
                It is also possible to load the resources from a webserver over http by specifying an absolute url in the resource reference. A relative reference that is based on the codebase of the application may work too, but it's been a while since I've done that, so I couldn't quickly write up the exact syntax required for that.
                • 5. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                  905754
                  Property file is still not accessible.

                  Edited by: Vikas Agrawal on Jan 18, 2012 1:34 AM
                  • 6. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                    905754
                    Hi jsmith ,

                    Thanks for your quick response. You have helped me lots :-) . As you suggested now I am using static method to get the path of all resource. Now Images are coming but still I am facing difficulties with property file.

                    In my application LoginPage is first class (also it extends Application).
                    I wrote static function as you suggested:

                    -----

                    static String getResource(String path) {
                    return LoginPage.class.getResource(path).toExternalForm();
                    }

                    -----

                    This is another method which is reading property file

                    -----

                    private boolean readServiceURLData() {
                    Properties pro = new Properties();
                    FileInputStream in;
                    boolean result = false;

                    try {
                    String path = getResource("config\");
                    File f1 = new File(path + "config.properties"));
                    in = new FileInputStream(f1);
                    pro.load(in);
                    ServiceCommunicator.HOSTNAME = pro.getProperty("service_hostname");
                    ServiceCommunicator.PORT = pro.getProperty("service_port");
                    ServiceCommunicator.SERVER_APP_Path = pro.getProperty("service_root");
                    in.close();

                    result = true;
                    System.out.println("result == "+result);
                    } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    } catch (IOException e) {
                    e.printStackTrace();
                    } catch (Exception e) {
                    e.printStackTrace();
                    }
                    return result;
                    }

                    -----

                    but is throwing exception FileNotFoundException

                    -----

                    java.io.FileNotFoundException: file:\E:\Projects\netbeans_projects\ConnecTedIN_UI_19Dec\build\classes\com\myorg\cin\ui\config\config.properties (The filename, directory name, or volume label syntax is incorrect)
                         at java.io.FileInputStream.open(Native Method)
                         at java.io.FileInputStream.<init>(FileInputStream.java:120)
                         at com.siemens.cin.ui.LoginPage.readServiceURLData(LoginPage.java:457)
                         at com.siemens.cin.ui.LoginPage.start(LoginPage.java:430)
                         at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
                         at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
                         at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
                         at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
                         at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
                         at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
                         at java.lang.Thread.run(Thread.java:662)

                    -----

                    My property file is available at "E:\Projects\netbeans_projects\ConnecTedIN_UI_19Dec\build\classes\com\myorg\cin\ui\config\" this location but still it is throwing exception.

                    Please suggest solution for it. This is the only issue left.

                    Thanks,
                    Vikas Agrawal

                    Edited by: Vikas Agrawal on Jan 18, 2012 1:31 AM

                    Edited by: Vikas Agrawal on Jan 18, 2012 1:34 AM
                    • 7. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                      jsmith
                      As you are deploying a client application over a network, you can't be sure that the computer that is executing the client application will have the property configuration file in the location you are trying to access (unless your application writes it there). Even if the file was there you would need to sign your deployed application to get access to it, which is generally an unnecessary hassle.

                      As an aside, use the code format specifiers (see the sticky thread at the top of the forums) for inlining code in forum posts as it makes the code more readable.
                      -----
                      To get this to work, you should package the configuration properties inside your jar with the rest of your resources, then you can load it via:
                      pro.load(LoginPage.class.getResourceAsStream("config/config.properties");
                      OR, if you can't package the configuration properties in the jar, for example because you want to vary the configuration independently of deploying a new jar, you can host the configuration on your webserver at a relative location to where you host your application and get it from there:
                      HostServices hosting = getHostServices();
                      pro.load(new URL(hosting.resolveURI(hosting.getDocumentBase(), "config/config.properties")).openStream());
                      For example, if the application jar is in tomcat's webapps/ROOT directory, and you want to access config/config.properties using HostServices, then place config.properties at webapps/ROOT/config/config.properties

                      Now, if you need to have user specific rather than application wide configuration, you will need to set up a web service to store the user specific configuration and read and write from that, or you would need to read and write the user configuration to the local drive using the jnlp FileService for an unsigned app or anywhere on the drive if you have a signed app.

                      However, it seems that your configuration is only storing a network service hostname, port and app path. If you can make the app path for your network service relative to where you deploy your application and record the relative path as a constant in your application, then you could just get the information, from:
                      HostServices hosting = getHostServices();
                      URL serviceURL = new URL(hosting.resolveURI(hosting.getDocumentBase(), APP_PATH));
                      ServiceCommunicator.HOSTNAME = serviceURL.getHost();
                      ServiceCommunicator.PORT = serviceURL.getPort();
                      ServiceCommunicator.SERVER_APP_PATH = serviceURL.getPath();
                      That way you wouldn't have to define, maintain and load a separate config.properties file.
                      Plus, having the service path relative to your application deployment makes the application easier to host elsewhere and negates the need to sign the application to get network services to work when the app is deployed in a webstart or browser embedded mode.

                      Edited by: jsmith on Jan 20, 2012 1:11 AM
                      I thought you could load a properties with just a string url, but it actually needs to be a stream, so I updated the call to load properties to take a stream.
                      Changed
                      pro.load(hosting.resolveURI(hosting.getDocumentBase(), "config/config.properties");
                      to
                      pro.load(new URL(hosting.resolveURI(hosting.getDocumentBase(), "config/config.properties")).openStream());
                      • 8. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                        905754
                        Hi jsmith,

                        Thanks for your response.

                        we need config file to read some more properties. So I tried second option first (Keep the config file outside the jar) but after building the application and running it from Netbeans 7.1 again I got same file not found exception. I have checked file was present in the specified location.

                        After that I tried first option to bundle the config file inside the jar file. this solution is working but while deployment in tomcat i have to sign the my application jar file as well as all dependent jar files. I have to manually sign all the jars. I tried the netbeans deployment option to sign the jar and when i run from netbeans it works fine but when I deploy it in Tomcat and then access it gives error that dependent jar files are not signed in java console.

                        Shall I create new thread for more discussion on it? My questions are:
                        Why I need to sign the jar file if it is not using the local resource?
                        Also I wanted to keep config file outside of the jar file as we change some configuration in future. Is any other alternative for that?

                        Thanks
                        Vikas Agrawal

                        Edited by: Vikas Agrawal on Jan 19, 2012 9:45 PM
                        • 9. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                          jsmith
                          Hi Vikas,
                          Why I need to sign the jar file if it is not using the local resource?
                          You don't need to sign the jar, just use getHostServices().resolveURI(getHostServices().getDocumentBase(), "path") where path is a relative, not absolute path and make sure your config property file is in the right place.
                          Also I wanted to keep config file outside of the jar file as we change some configuration in future.
                          That's fine, you can do that if you want.
                          Is any other alternative for that?
                          The suggestion in this post should work, I don't have an alternative.
                          Shall I create new thread for more discussion on it?
                          Create a new thread with a targeted question if necessary.

                          I had a minor error in the hostService lookup call which I fixed in the above post.

                          I create an app which loads a config file hosted in tomcat separately from the webstart or browser embedded app jar.
                          I put some logging info around the getHostServices call to help debug any path resolution issues.
                          final HostServices hosting = getHostServices();
                          final Properties props = new Properties();
                          URL configUrl = null;
                          try {
                            configUrl = new URL(hosting.resolveURI(hosting.getDocumentBase(), "config/config.properties"));
                            System.out.println("Loading config properties from: " + configUrl);
                            props.load(configUrl.openStream());
                            props.list(System.out);
                          } catch (IOException e) {
                            System.out.println("Unable to load config properties from: " + configUrl);
                          }
                          When placed the application and configuration on the server and accessed the application using a browser, in the java console I got:
                          Loading config properties from: http://localhost:8080/config/config.properties
                          -- listing properties --
                          xyzzy=123
                          which is the contents of the config property file I created.
                          • 10. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                            sumani
                            Thanks Smith for the consistent involvement.

                            I have tried deploying in as a web application(I did not bother about the signing jar as I am trying a test).

                            It works fine when accessed in IE 8.0 from Windows XP 32 bit.

                            I have problems in accessing the URL in Windows 7 operating system. JavaFX 2.0.1 runtime is already available in the machine. In fact I have tried with 2 different machines with Windows 7.

                            Is there a specific configuration/setting I need for different operating systems?
                            • 11. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                              jsmith
                              Is there a specific configuration/setting I need for different operating systems?
                              No, though I would advise using a later version than 2.0.1 as many bugs have been fixed since that release, use 2.0.2 or one of the 2.1 beta builds.
                              Also, try a couple of different browsers in case the issue is browser plugin specific.
                              • 12. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                                817264
                                What exactly is the problem?

                                Do you get "need to install Java or JavaFX" message? or it starts but fails?
                                Please enable detailed trace and provide all the data collected.
                                See http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/plugin.html#gcexdf for details on how to collect it.
                                • 13. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                                  905754
                                  HI jsmith,

                                  Thanks for your response.
                                  Now I am able to keep the config file outside the jar.
                                  But still I have to sign the jar files.
                                  I am using two dependent third party jar files. I have to sign those jars as well as my application jar file.
                                  Is it because of those two dependent jar files I need to sign all jars?
                                  If yes then any solution is available so that I need not to sign the jars because I don't have trusted license certificates. I am generating self-signed certificates from command which are valid only for 6 months only.

                                  Thanks,
                                  Vikas Agrawal
                                  • 14. Re: How to deploy Javafx 2.0 Application in web server and accsess by URL
                                    jsmith
                                    Is it because of those two dependent jar files I need to sign all jars?
                                    I can't say. The best way to determine that is to follow the excellent instructions at the link Igor provided. Igor is much more knowledgeable in these matters than I am.
                                    If you see security exceptions occurring after tracing is enabled, then you can tell from the location of the exception whether the issue is in your code or in the third party code and perhaps modify the code or work around the issue to prevent it from occurring.

                                    If you sign your code, then your should also sign all code in the dependent jars otherwise you get security warnings for "Mixed code" exceptions, which would be really confusing to a user.
                                    If yes then any solution is available so that I need not to sign the jars because I don't have trusted license certificates.
                                    Read http://docs.oracle.com/javase/tutorial/deployment/applet/security.html and http://mindprod.com/jgloss/applet.html#RESTRICTIONS to understand what unsigned applets can and cannot do and see what operations require you to sign your browser deployed or webstart application.
                                    I am generating self-signed certificates from command which are valid only for 6 months only.
                                    I believe users can still use the self-signed code even after 6 months, it just warns them that the certificate has expired. But, if it is self-signed then they will get a warning anyway, so I don't think that is much of an additional concern.
                                    Even if you sign it with a CA issued certificate, users still get a security pop up at least once asking if they want to trust the code or the issuer - the experience is a bit nicer than a self-signed certificate though, which will pop-up a warning every time the application is used and say the publisher is UNKNOWN. If you search on the web you can get an Organization or Individual Code Signing certificate for about a hundred dollars a year.