1

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

我可以通过包含的 ANT 构建文件运行我的 JAR。它运行,并显示输出,以及各种时髦的东西。现在,如果我尝试通过命令行运行同一个 JAR 文件,java -jar JARFile.jar它就会发出声音。它给出了一个NullPointerException. 有趣,对吧?

有问题的代码行如下,特别是第 3 行:

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

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

提醒:此构建脚本过去创建了可运行的 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>
4

0 回答 0