The Enterprise Side of JavaFX: The Missing Part

The introduction you most likely don’t need

If you’re just a bit like me, you adore Java EE and all it has to offer. Java EE makes remoting from an application client a breeze if you stick to ordinary Java SE Swing based application clients, Java Web Start and dependency injection. Then all you have to do is to annotate a static field in your application client’s Main class and then go to work with this variable and everything just works.

But Java SE 7 update 6 happened which began the Oracle tradition of including JavaFX runtime libraries and utilities. Oracle also announced they will drop Swing in favor of JavaFX and indeed that is a good thing since Swing, which is today some kind of a standard for Java GUI:s, hasn’t been updated for a really long time and lost its competitive edge to other frameworks and languages. JavaFX on the other hand is new technology; featuring nice stuff like hardware acceleration and touch capabilities. I’m sure you can google the rest ;)

So what is the problem?

You cannot package a JavaFX client in an EAR package and then do dependency injection as if the application client is just another regular enterprise application client. Well, in theory I don’t think there’s anything there to stop you. I just haven’t got it to work in a straight forward way..

..until I found the solution I will present in this post/tutorial. The solution that will package a JavaFX application in an EAR package and then use resources from your Java EE application server with dependency injection.

You will not have to do any JNDI lookup programmatically and it even works with role based authentication as will be shown! All because your application client will for starters be a standard Java SE application that in his turn, when everything has been setup, starts a JavaFX application. The trick is not ugly, it is a pure JavaFX solution with kept benefits. Your ordinary thread will call javafx.application.Application#launch(Class, String) which will launch and setup the JavaFX application you’re used to. No strings attached! The solution is to be considered more of a packaging trick than anything else. If I haven’t made myself clear enough: The end GUI is a complete JavaFX application and nothing will be lost. You will even maintain your standard working model with separate FXML files for the structure of your GUI elements and controller classes in separate files that handle the business logic behind the GUI elements. You will not have to build scripts for Maven or Ant. You will not have to pass eye straining arguments to the Java- compiler or virtual machine. The solution will be completely built using only wizards from the NetBeans IDE and some minor code customizations.

Alternative solutions

One apparent way to connect a remote JavaFX desktop application with your application server is to use a HTTP wired web service. Oracle has demonstrated consuming a RESTful web service from a JavaFX application here, here and here. Oh yes, there are some “parts” here and here too. However, REST is not the only enterprise communication model out there, although as of today, it is the only remoting model you can found demonstrated on the Internet. For a particular project of mine, I found that JMS was a better alternative. I also needed remote access to an EJB. Alas invoking server side resources through JNDI proved to be error prone and a real hassle. But with Java Web Start and dependency injection, the user won’t even have to supply an IP and port that maps to the server.

What we will produce

In this tutorial, you will build a JavaFX application, packaged together with an EJB in an EAR package and have the application client launch through Java Web Start. Then the client shall login and get a role before he makes a remote call to a server side EJB. Here is a screenshot of the finished application:

.

JavaFX Application Client

.

If you really want to know: My setup is NetBeans IDE 7.3 and GlassFish 3.1.2.2 (build 5). All running on Windows 8 x64. And one more thing before we begin; I use a tremendously simplified package naming scheme. I use only one package name shared across all my projects/modules for this tutorial. IRL, that practice is much discouraged.

What you will find in this tutorial and related pages:

  • Create a new Java EE Enterprise Application (page 2)
  • Create a shared library (page 3)
  • Create a new stateless Enterprise JavaBean (page 4)
  • Setup the role in GlassFish (page 5)
  • The JavaFX client template (page 6)
  • The Swing client (page 7)
  • Setup the Swing client (page 8)
  • Configure GlassFish (page 9)
  • Write some Java logic (page 10)
  • Build, deploy and run! (page 11)
.Create a new stateless Enterprise JavaBeanCreate a new stateless Enterprise JavaBeanCreate a new stateless Enterprise JavaBeanBuild, deploy and ru

9 thoughts on “The Enterprise Side of JavaFX: The Missing Part

  1. mauro

    hi your blog it is very very interessant.
    But how you resolve the problem with the call at remote ejb or application client on a glassfish behind a firewall ?

    For example i would download and use the appclient and the glassfish it is on a another macheine extenal at the internal net but on intenet .
    go at :
    https://www.java.net//forum/topic/glassfish/glassfish/ejb-remote-deployed-gf-31-behind-nat-unaccessible
    for view the problem .
    tanl you very much for your attention
    Mauro

    Reply
    1. martinandersson.com Post author

      I have no solution to your problem. Actually I haven’t read your URL:s, just looked over them briefly. However, all kinds of remoting is always troublesome if you’re behind a firewall. HTTP wired web services has no issue since they talk on port 80, almost always unblocked, and I believe this is the chief reason they have gained so much in popularity. So the moment a firewall gives you a headache, just expose the EJB as a web service. There is nothing stopping you from combining @WebService with @Stateless.

      Reply
  2. Pingback: JavaFX links of the week, July 22 // JavaFX News, Demos and Insight // FX Experience

  3. AFX

    Grymt martin! Keep it up!
    Nice kodat, och nu ska jag ta och kolla igenom resten av bloggen;) Man blir ju sugen på att börja koda själv fan. Verkar skitkul.

    Reply
  4. R

    Hi. Thanks for this blog post. It is really helpful. I’ve managed to run it inside the Netbeans. Unfortunately I’ve failed to do it outside the Netbeans. I used Glassfish appclient. I have the following errors……


    Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    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 org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:212)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.joy.azura.Sly.main(Sly.java:23)
    Caused by: java.lang.ClassNotFoundException: javafx.application.Application
    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 org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:212)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more

    In line 14 (Sly.java:23) my code is

    javafx.application.Application.launch(SlyFX.class, args);

    I’m able to execute it inside the Netbeans. Thus, I think there is a way to do it outside of the Netbeans.

    Reply
    1. martinandersson.com Post author

      Could it be that the Application Client Container (ACC) cannot find your JavaFX runtime? And when you say “outside of NetBeans”, I hope you mean using Java Web Start? Please review page 8 and see if it helps. And be sure to upgrade your JDK installed if it is anywhere below 1.7.0_6.

      Reply
      1. R

        Thanks for your prompt reply. I was using the old next to obsolete method. “asadmin …get-client-stubs… appclient …” so you will have the lighter Glassfish on the client’s computer. Then to execute the Client side type “appclient.bat -jar MyApp-Client.jar” in command line. I have a fat client which needed to work offline. Fortunately requirements have changed. Now I have decided to switch to RESTful Web Service. That one has much better option. Thanks again.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>