7

我有一个结构如下的项目:

主项目
-libary-1
-library-2
-i-need-javafx-app-here

所以,我需要在我的 Maven 项目中使用 JavaFX 2 应用程序,它应该使用 library-1 和 library-2 及其依赖项。我找不到 JavaFX 2 项目的任何 maven 原型,并且我可以找到有关如何通过 Maven 3 构建 JavaFX 应用程序的任何足够信息。我不需要任何网络部署,它只会部署桌面应用程序。

那么,任何人都可以帮助解决这个问题吗?

升级版:

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

当我尝试运行由pgras方式构建的应用程序时发生异常。

4

3 回答 3

5

所以我猜你已经使用Java 7,我不知道你需要什么,编译你只需要对javafx的依赖,所以在你的javafx应用程序的pom中:

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

并在您的 maven settings.xml 添加(并适应您的系统的路径):

<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>

编译和制作jar文件应该足够了......如果不告诉我,我会发布更多信息......

于 2012-05-10T11:06:50.227 回答
2

非常感谢,这个链接很有帮助。所以,现在我的 pom 看起来像这样:

<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>

因此,现在可以从命令行“java -jar myapp.jar”运行应用程序,但我还没有测试它如何在依赖项下运行。

于 2012-05-15T04:04:14.603 回答
0

我添加了另一个答案,因为它很长,它显示了我如何生成签名的 jar 和 JNLP webstart 文件。

因此,首先我创建了一个能够对 jar 进行签名的证书(您将被要求输入一些信息,我使用“superpass”作为密码,看看它在 pom 中的使用位置):

cd src/main/java
mkdir jnlp
cd jnlp
keytool -genkey -alias signFiles -keystore keystore.jks

然后我使用这个 pom.xml(它有一个父 pom,但它不包含任何与 javafx 相关的内容):

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
    <artifactId>testfx</artifactId>
    <groupId>ch.pgras</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<groupId>ch.pgras</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hellofx</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>javafx</artifactId>
        <version>2.0</version>
        <systemPath>${javafx.rt.jar}</systemPath>
        <scope>system</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <configuration>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                </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="jfxdeploy" classname="com.sun.javafx.tools.ant.DeployFXTask"
                                     classpathref="maven.plugin.classpath"/>

                            <taskdef name="jfxsignjar" classname="com.sun.javafx.tools.ant.FXSignJarTask"
                                     classpathref="maven.plugin.classpath"/>

                            <jfxdeploy width="800" height="600" outdir="${project.build.directory}/deploy"
                                       outfile="${project.build.finalName}">
                                <info title="${project.name}"/>
                                <application name="${project.name}" mainclass="webmap.WebMap"/>
                                <resources>
                                    <fileset dir="${project.build.directory}" includes="*.jar"/>
                                    <fileset dir="${project.build.directory}/dependency"
                                             includes="*.jar"/>
                                </resources>
                                <platform javafx="2.0">
                                    <jvmarg value="-Xms64m"/>
                                    <jvmarg value="-Xmx256m"/>
                                </platform>
                            </jfxdeploy>
                            <jfxsignjar destdir="${project.build.directory}/deploy"
                                        keystore="${project.basedir}/src/main/java/jnlp/keystore.jks"
                                        storepass="superpass" alias="signFiles" keypass="superpass">
                                <fileset dir="${project.build.directory}/deploy" includes="*.jar"/>
                            </jfxsignjar>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.oracle.javafx</groupId>
                    <artifactId>ant-javafx</artifactId>
                    <version>2.0</version>
                    <systemPath>${ant.javafx.jar}</systemPath>
                    <scope>system</scope>
                </dependency>
            </dependencies>
        </plugin>

    </plugins>
</build>

最后我运行 mvn install,结果将在 target/deploy...

祝你好运 :)

于 2012-05-11T13:18:47.633 回答