从用Groovy编写的代码创建JavaFX jar文件时出错

时间:2016-05-21 13:42:52

标签: javafx groovy ant

我尝试使用以下build.xml从Groovy编写的源代码创建一个独立的jar文件:

<?xml version="1.0" encoding="UTF-8"?>

<project name="build" default="build" basedir="." xmlns:fx="javafx:com.sun.javafx.tools.ant">

  <property name="groovy" value="C:/groovy-2.4.5"/>
  <property name="source" value="source"/>
  <property name="binary" value="binary"/>
  <property name="distribution" value="distribution"/>
  <property name="data" value="data"/>
  <property name="name" value="application"/>
  <property name="class" value="ziphil.main.Launcher"/>

  <path id="groovy.classpath">
    <fileset dir="${groovy}/embeddable/"/>
  </path>

  <path id="compile.class.path">
    <fileset dir="${groovy}/lib/"/>
  </path>

  <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpathref="groovy.classpath"/>

  <target name="clean">
    <mkdir dir="${binary}"/>
    <mkdir dir="${distribution}"/>
    <delete>
      <fileset dir="${binary}" includes="**/*"/>
      <fileset dir="${distribution}" includes="**/*"/>
    </delete>
  </target>

  <target name="compile" depends="clean">
    <groovyc srcdir="${source}" destdir="${binary}" classpathref="compile.class.path" encoding="UTF-8"/>
    <copy todir="${binary}/${data}">
      <fileset dir="${source}/${data}"/>
    </copy>
  </target>

  <target name="build" depends="compile">
    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml" uri="javafx:com.sun.javafx.tools.ant" classpath="${java}/lib/ant-javafx.jar"/>
    <fx:application id="id" name="${name}" mainClass="${class}"/>
    <fx:resources id="appRes">
      <fx:fileset dir="${distribution}" includes="${name}.jar"/>
    </fx:resources>
    <fx:jar destfile="${distribution}/${name}.jar">
      <fx:application refid="id"/>
      <fx:resources refid="appRes"/>
      <fileset dir="${binary}"/>
    </fx:jar>

  </target>

  <target name="run" depends="build">
    <java jar="${distribution}/${name}.jar" fork="true"/>
  </target>

</project>

但我有一个错误,说在尝试运行创建的jar文件时(在上面的XML的run目标中)找不到主类。我该怎么做才能解决这个错误?

1 个答案:

答案 0 :(得分:0)

您可能必须将运行时类路径添加到应用程序所依赖的所有jar中。我想您至少需要将compile.class.path包含在运行时类路径中:

<target name="run" depends="build">
  <java jar="${distribution}/${name}.jar" fork="true" classpathref="compile.class.path"/>
</target>