3

I've been trying to fix this for ages but here goes : I have a text file that gets loaded fine when I run my java code with getResourceAsStream within Eclipse.

The text file is in the same package dir as the class that uses it. however , when I run the same code from command line with ant , getResourceAsStream returns null.

I figure it is class path related but I can't seem to get it going from tinkering with my build.xml or directory structure

Any help appreciated, thanks, Jamesie

4

1 回答 1

2

我遇到了同样的问题,我花了一段时间才解决它。我还认为问题来自getResourceAsStream(),或来自参数中的相对路径,但

它比这简单得多:文件不存在。

您可以检查是否需要:转到您使用 ant 生成的.jar 文件,然后解压缩它(使用 7zip、WinRAR 或其他...)。您将看到您的 config.properties 文件不存在。

事实上,如果只是<jar />在您的build.xml文件(ant 的输入)中使用,它只会压缩.java文件!您还需要使用复制:

<target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
        <copy todir="${classes.dir}">
            <fileset dir="${res.dir}"
                     includes="**/*.properties,**/*.xml,**/*.png,**/*.jpg" />
            <!-- I advise you to also include .xml .jpg, ... or any file you need that doesn't end with .java--> 
        </copy>
</target>

就是这样,您现在可以通过再次解压缩来检查 config.properties 是否包含在您的最终.jar文件中。

于 2017-10-31T13:14:47.523 回答