JavaFX 2, Maven, and Spring on Netbeans

I recently wanted to toy with creating a user interface for a standalone application that I was developing. After getting the initial project setup with Maven in Netbeans, I decided to try to integrate Spring into the mix in order to leverage Spring's dependency injection facilities. This is where my headache started.

I immediately started encountering issues where Spring classes where not being found due to how the default Netbeans+Maven+Spring project was setup. After a day or so of searching around the internet and trying different approaches with including external dependencies, I finally found an approach that worked using the JavaFX Maven Plugin and OneJar.

One of the first issues that I encountered was the project running into an exception resolving my Spring dependencies resulting in "Exception: ClassNotFoundException : Org.Springframework.Web.Context.ContextLoaderListener". References to this show up all over the internet referring to class path resolution issues, missing dependencies, etc. Basically the executable jar couldn't find the dependencies. I tried a few solutions to package the dependecies differently, running into security issues, more ClassNotFoundExceptions, and all out breakages of the project. Eventually I decided to start with a clean pom and start building.

The first stop JavaFX Maven plugin. According to the web site "As of Java 7 update 7, JavaFX is now 'co-bundled' with the JDK, so when you install the JDK, JavaFX is automatically installed with it. Oracle however have not fully finished this co-bundling and have left this in a somewhat annoying half-done state. The JavaFX libraries are included with the JRE, but are NOT put on the classpath by default. The above Maven command finishes the job so that your system is fully setup to use JavaFX."

In order to get the JavaFX Maven plugin to work (on Windows 7), I had to install maven, setup my environment variables, and ensure I could execute maven from the command line. Generally I just use maven through my IDE, in this case I needed it from the shell. Instructions on how to install Maven in windows can be found at Maven in 5 Minutes.

After installing maven, from a command prompt you need to run:

mvn com.zenjava:javafx-maven-plugin:2.0:fix-classpath

Be aware, this will likely need to be run from a command prompt that has been run as administrator or you will get an error. The easiest way to make this happen is to click the start button, enter "cmd" in the search field, right click on the resulting icon in the programs area, and select "Run as administrator" from the context menu. The script will run and prompt you once at which point I selected the yes option.

Once adding JavaFX to the classpath was complete, I added the plugin to my pom:

            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mainClass>org.oclc.verifications.console.MainApp</mainClass>
                </configuration>
            </plugin>

Once this was complete I added the on-jar plugin to the pom to package all the depenencies in the distributable jar. After view a fiew examples and configurationes, I landed on the following entry in my pom:

    <pluginRepositories>
        <pluginRepository>
            <id>onejar-maven-plugin.googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <mainClass>org.console.MainApp</mainClass>
                            <!-- Optional, default is false -->
                            <attachToBuild>true</attachToBuild>
                            <!-- Optional, default is "onejar" -->
                            <classifier>onejar</classifier>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

          ......

        </plugins>
    </build>

After setting up these two plugins and verifying the pom was in good shape, I did a quick clean and build from Netbeans with success, however, there are some changes that needed to be made to the project properties in order to get the JavaFX application to actually run. With the current configuration (assuming you started with a base JavaFX Maven project in Netbeans), the project with compile and build, but will not execute due to the standard Maven commands that are configured in the project Actions section. There are three actions that I changed (though there may be more) in order to get the project to clean, build, and run as expected.

According to the JavaFX Maven Plugin website, "To build a JavaFX-aware, executable JAR, with JavaFX pre-loader support" and run it using the plugin you need to use the following commands: 

mvn clean jfx:jar

and

mvn jfx:run

In Netbeans, these commands are found by doing the following:

  • In the projects tab, right click the project node (head) and select properties
  • Click the "Actions" option in the Categories pane
  • In the Acions pane change the Clean project and Clean and Build project execute goal from clean to clean jfx:jar
  • In the Actions pane change teh Run project execute goal from run to jfx:run

​After making these changes in the project properties I was able to successfully run the JavaFX-Maven-Spring intregrated project as expected with not errors.