用JAR解决build.xml问题



当我让ant构建java项目(默认操作"run")时,程序正常启动。无警告或错误。

但是当我从终端运行由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时,它可以工作,甚至在控制台上。

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

在您的run目标中,类路径不仅是您的jar,还包括refid=classpath路径中的任何内容。

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

相关内容

  • 没有找到相关文章

最新更新