Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javafx application in maven project

I have a project with structure like this:

main-project
-libary-1
-library-2
-i-need-javafx-app-here

So, i need a JavaFX 2 application in my maven project, and it should use library-1 and library-2 with its dependencies. I can't find any maven archetypes for JavaFX 2 projects and i cand find any adequate information about how i can build JavaFX app by Maven 3. I don't need any deployment to web, it will only desktop app.

So, can anybody help with this problem?

UPD:

java.lang.NoClassDefFoundError: javafx/application/Application
...
Caused by: java.lang.ClassNotFoundException: javafx.application.Application

exception is occured when i try to run application, which is builded by pgras way.

like image 825
Anton Avatar asked May 10 '12 10:05

Anton


People also ask

How do I use JavaFX in Maven?

Open the Maven Projects window ( View -> Tool Windows -> Maven ) and click on HelloFX -> Plugins -> compiler -> compiler:compile to compile the project, and click on HelloFX -> Plugins -> javafx -> javafx:run to execute the project.

Does Maven support JavaFX?

If you want to develop JavaFX applications using Maven, you don't have to download the JavaFX SDK. Just specify the modules and the versions you want in the pom. xml , and the build system will download the required modules, including the native libraries for your platform.

What is JavaFX application application?

A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content. Example 1-1 creates the stage and scene and makes the scene visible in a given pixel size.

How do I add JavaFX to an existing project?

Click on the File menu and select Project Structure . In the dialog that appears, select the Libraries tab and click the + icon to add a new Java library: Find your javafx-sdk folder and select the lib subfolder: Click the OK button to complete the process.


2 Answers

So I guess you already use Java 7, I d'ont know exactly what you need, for compiling you will need only the dependency to javafx, so in your pom for the javafx app:

<dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>javafx</artifactId>
        <version>2.0</version>
        <systemPath>${javafx.rt.jar}</systemPath>
        <scope>system</scope>
    </dependency>
</dependencies>

And in your maven settings.xml add (and adapt path to your system):

<profile>
  <id>javafx</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <javafx.rt.jar>C:\Program Files (x86)\Oracle\JavaFX 2.0 SDK\rt\lib\jfxrt.jar</javafx.rt.jar>
    <ant.javafx.jar>C:\Program Files (x86)\Oracle\JavaFX 2.0 SDK\tools\ant-javafx.jar</ant.javafx.jar>
  </properties>
</profile>

It should be enough to compile and make jar file... If not tell me and I'll post more info...

like image 103
pgras Avatar answered Sep 29 '22 11:09

pgras


Thank you a lot, this link is helpful. So, now my pom looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>info.kosmonaffft</groupId>
    <artifactId>javafx-maven-example</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>
    <name>javafx-maven-example</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <excludeArtifactIds>junit,hamcrest-core</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <taskdef name="jfxjar" classname="com.sun.javafx.tools.ant.FXJar" classpathref="maven.plugin.classpath"/>
                                <jfxjar destfile="${project.build.directory}/${project.build.finalName}">
                                    <fileset dir="${project.build.directory}/classes"/>
                                    <application name="${project.name}" mainClass="info.kosmonaffft.jfxexample.App"/>
                                    <resources>
                                    </resources>
                                </jfxjar>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle.javafx</groupId>
                        <artifactId>ant-javafx</artifactId>
                        <version>2.0</version>
                        <scope>system</scope>
                        <systemPath>C:\Program Files\Oracle\JavaFX 2.1 SDK\tools\ant-javafx.jar</systemPath>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javafx</groupId>
            <artifactId>javafx-rt</artifactId>
            <version>2.1</version>
            <scope>system</scope>
            <systemPath>C:\Program Files\Oracle\JavaFX 2.1 SDK\rt\lib\jfxrt.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

So, it is possible to run application from command line "java -jar myapp.jar" now, but i have not tested how it will runs with dependencies.

like image 34
Anton Avatar answered Sep 29 '22 11:09

Anton