JAR的Ant build.xml问题

时间:2011-11-11 14:51:06

标签: ant jar

当我让ant构建java项目时(默认操作"运行")程序开始正常。没有警告或错误。

但是当我运行由ant构建的JAR时,从终端(java -jar ...)一切看起来都很好,直到我使用外部库中的任何功能(例如swingx,date chooser)。提出了一个大规模的解释:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.jdesktop.swingx.plaf.basic.BasicMonthViewUI.getTraversableGridPositionAtLocation(BasicMonthViewUI.java:906)
    at org.jdesktop.swingx.plaf.basic.BasicMonthViewUI$Handler.mousePressed(BasicMonthViewUI.java:1723)
    at java.awt.Component.processMouseEvent(Component.java:6501)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

这是我的ant build.xml:

<project name="Jamm" basedir="." default="run">

<property name="src.dir" value="src" />
<property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="jar.dir" value="${build.dir}/jar" />
<property name="lib.dir" value="lib" />
<property name="main-class" value="jamm.Main" />
<buildnumber file="build.num" />
<property name="res.dir.name" value="res" />
<property name="res.dir" value="${src.dir}/${res.dir.name}" />
<property file="${res.dir}/version_num.properties" />
<property name="jar.filename" value="${ant.project.name}_v${versionnumber}-b${build.number}.jar" />

<tstamp>
    <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar" />
</path>

<target name="clean">
    <delete dir="${build.dir}" />
</target>

<target name="compile" depends="clean">
    <mkdir dir="${classes.dir}" />
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="true"/>
</target>

<target name="dist" depends="compile">
    <mkdir dir="${jar.dir}" />
    <jar destfile="${jar.dir}/${jar.filename}" basedir="${classes.dir}">
        <restrict>
            <name name="**/*.class" />
            <archives>
                <zips>
                    <fileset dir="${lib.dir}" includes="**/*.jar" />
                </zips>
            </archives>
        </restrict>
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
            <attribute name="Built-By" value="${user.name}" />
            <attribute name="Build-Version" value="v${versionnumber}_b${build.number}" />
            <attribute name="Built-Date" value="${TODAY}" />
        </manifest>

        <fileset dir="${src.dir}">
            <include name="${res.dir.name}/**/*" />
        </fileset>

    </jar>
</target>

<target name="run" depends="dist">
    <java fork="true" classname="${main-class}">
        <classpath>
            <path refid="classpath" />
            <path location="${jar.dir}/${jar.filename}" />
        </classpath>
    </java>
</target>

当我从Eclipse导出可运行的JAR时,即使在控制台上也能正常工作。

1 个答案:

答案 0 :(得分:2)

在终端中执行时,是否使用与run目标中相同的类路径执行此操作?

在你的run目标中,类路径不仅是你的jar,也是refid = classpath的路径中的任何内容。

由于您说使用java -jar在终端中执行,我认为这证实了这两个上下文的类路径不同。