Jar通过Ant运行,但不是独立的

时间:2012-07-19 13:17:10

标签: java ant jar nullpointerexception

所以,我有一个有趣的问题:

我可以通过包含的ANT构建文件运行我的JAR就好了。它运行,并显示输出,以及各种常规的东西。现在,如果我尝试通过命令行java -jar JARFile.jar运行这个相同的JAR文件,它就会嘶哑。它给出NullPointerException。好玩,对吧?

违规行代码如下:第3行:

File dir = new File("media/bgs/");
children = dir.list();
int num = (((int) (Math.random()*children.length)));
String choice = children[num];

此代码从jar中的media/bgs文件和类路径中选择随机背景图像。

提醒:此构建脚本过去已创建可运行的JAR,目前,这些JAR将通过ANT构建文件运行。

思想?

ANT构建文件

<property name="dir.src" value="src" />
<property name="dir.build" value="build" />
<property name="dir.data" value="data" />
<property name="dir.media" value="media" />
<property name="dir.lib" value="lib" />
<property name="dir.dist" value="dist" />
<property name="name.jar" value="EmpView2.jar" />
<property name="jarpath" value="${dir.dist}/${name.jar}" />

<property file="data/build_info.properties" />
<property name="build.number" value="${build.major.number}.${build.minor.number}.${build.revision.number}" />

<path id="build-classpath">
    <pathelement location="${dir.dist}/${name.jar}" />
    <fileset dir="${dir.lib}" includes="**/*.jar" />
    <fileset dir="${dir.src}" includes="**/*.jar" />
    <fileset dir="${dir.data}" includes="**/*" />
    <fileset dir="${dir.media}" includes="**/*" />
</path>



<target name="current-number">
    <echo>Current build number:${build.number}</echo>
</target>




<target name="dist">
    <antcall target="minor">
    </antcall>
</target>

<target name="major">
    <propertyfile file="data/build_info.properties">
        <entry key="build.major.number" type="int" operation="+" value="1" pattern="00" />
        <entry key="build.minor.number" type="int" value="0" pattern="00" />
        <entry key="build.revision.number" type="int" value="0" pattern="00000" />
    </propertyfile>
</target>

<target name="minor">
    <propertyfile file="data/build_info.properties">
        <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00" />
        <entry key="build.revision.number" type="int" value="0" pattern="00000" />
    </propertyfile>
</target>

<target name="revision">
    <propertyfile file="data/build_info.properties">
        <entry key="build.revision.number" type="int" operation="+" value="1" pattern="00000" />
    </propertyfile>
</target>

<target name="all">
    <propertyfile file="data/build_info.properties">
        <entry key="build.major.number" type="int" operation="+" value="1" pattern="00" />
        <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00" />
        <entry key="build.revision.number" type="int" operation="+" value="1" pattern="00000" />
    </propertyfile>
</target>






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

<target name="prepare">
    <mkdir dir="${dir.build}" />
    <mkdir dir="${dir.dist}" />
</target>


<target name="compile" depends="prepare">

    <depend srcDir="${dir.build}" closure="true" />


    <!-- Capture the path as a delimited property using the refid attribute -->
    <property name="myclasspath" refid="build-classpath" />
    <!-- Emit the property to the ant console -->
    <echo message="Classpath = ${myclasspath}" />


    <javac srcdir="${dir.src}" destdir="${dir.build}" classpathref="build-classpath" debug="on" includeantruntime="true" />

    <unzip dest="${dir.build}">
        <fileset dir="${dir.lib}" includes="**/*.zip" />
        <fileset dir="${dir.lib}" includes="**/*.jar" />
    </unzip>

</target>

<target name="jar" depends="compile">

    <manifestclasspath property="lib.list" jarfile="${dir.dist}/${name.jar}">
        <classpath refid="build-classpath" />
    </manifestclasspath>

    <antcall target="revision" />

    <jar destfile="${jarpath}" basedir="." includes="${dir.lib}/*" filesetmanifest="mergewithoutmain">
        <manifest>
            <attribute name="Main-Class" value="com.baesystems.ev2.EmpowerView" />
            <attribute name="Class-Path" value="${lib.list}" />
        </manifest>
        <fileset dir="${dir.build}" includes="**/*" excludes="META-INF/*.SF" />
        <fileset dir="." includes="${dir.media}/**/*" />
        <fileset dir="." includes="${dir.data}/*" />
    </jar>

    <chmod file="${dir.dist}/${name.jar}" perm="+x" />
</target>

<target name="run">
    <echo message="Attempting to run!" />
    <java jar="${jarpath}" fork="true" />
    <echo message="JAR launched." />
</target>

<target name="compile-jar-run">
    <antcall target="jar" />
    <antcall target="run" />
</target>

<target name="clean-compile-jar-run">
    <antcall target="clean" />
    <antcall target="compile-jar-run" />
</target>

0 个答案:

没有答案