未找到文件异常-从jar执行时资源文件夹中的数据文件引发异常

Jas*_*son 1 java jar executable-jar maven

我有一个Swing应用程序,试图将其打包到可运行的JAR文件中。它的DAO功能的一部分是以CSV格式读写内部的.dat文件src/main/resources/dictData.dat

我的问题是,每次尝试运行jar时,我都会

java.io.FileNotFoundException: file:/Users/jason/projects/test-dict/target/
    dictionary-jar-with-dependencies.jar!/dictData.dat 
    (No such file or directory)
Run Code Online (Sandbox Code Playgroud)

从命令行。这是通过一个罐子mvn packagemaven-assembly-plugin规格

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.test.dictionary.init.AppInit</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

我有一个FileIO类实现,可以处理对文件的读写。

public class FileIO implements IO{

    private static final String DICTIONARYFILE = "dictData.dat";
    private File dataFile;
    private Writer dataWriter;
    private Reader dataReader;

    @Override
    public Map<String, Word> loadDataFile(){
        ClassLoader classLoader = getClass().getClassLoader();
        Map<String, Word> dictMap = new HashMap<>();

        try {
            //Open file connection and read stream
            dataFile = new File(classLoader.getResource(DICTIONARYFILE).getFile());
            dataReader = new FileReader(dataFile);

            //CSV parsing code here
        } catch (NullPointerException | IOException | NumberFormatException e){
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我只在执行时收到此错误

java -jar dictionary-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)

里面/Users/jason/projects/test-dict/target/。如果我通过IDE运行,则不会出现任何错误。

问题是,我可以dictData.dat通过vim dictionary-jar-with-dependencies.jar以下方式在罐子的末端看到:

...
... 
com/test/dictionary/utils/FileIO.class
com/test/dictionary/utils/HttpUtils.class
com/test/dictionary/utils/IO.class
dictData.dat
META-INF/maven/com.test/
META-INF/maven/com.test/dictionary/
META-INF/maven/com.test/dictionary/pom.xml
META-INF/maven/com.test/dictionary/pom.properties
Run Code Online (Sandbox Code Playgroud)

因此,除了将数据文件移动到某个test-dict/com/text/dictionary/data位置外,如何解决此问题?

VGR*_*VGR 5

您不能将返回的URL转换classLoader.getResource(DICTIONARYFILE)为文件名,因为应用程序资源通常不是实际文件。即使可以,URL.getFile()也是错误的做法。使用此代替:

URL dataFile = classLoader.getResource(DICTIONARYFILE);
dataReader = new InputStreamReader(dataFile.openStream());
Run Code Online (Sandbox Code Playgroud)

详细说明:

当您的程序从.jar文件运行时(除某些开发环境外,几乎所有程序都可以这样做),Class.getResource和ClassLoader.getResource方法返回一个URL,该URL是jar URL。jar URL是具有以下格式的特定于Java的URL方案:

jar:jar文件的URL !jar入口

您的情况是.jar文件位于/Users/jason/projects/test-dict/target/dictionary-jar-with-dependencies.jar。以URL形式表示file:/Users/jason/projects/test-dict/target/dictionary-jar-with-dependencies.jar

您请求的jar文件中的条目/dictData.dat。因此,您的getResource电话返回的网址是:

jar:文件:/Users/jason/projects/test-dict/target/dictionary-jar-with-dependencies.jar!/dictData.dat

URL.getFile()不按照您的想法去做。特别是,URL.getFile() 不会将URL转换为文件名。它仅返回URL 的路径部分。路径部分不是文件名;它只是URL的任何部分,位于方案/权限/主机/端口之后,直至第一个问号('?')或哈希('#')。

对于jar URL,路径部分是4个字符之后的所有内容jar:。因此,您有效地致电new File("file:/Users/jason/projects/test-dict/target/dictionary-jar-with-dependencies.jar!/dictData.dat")

要从URL读取,您不应尝试将其转换为文件,也不应假定URL是文件:URL。如您所见,通常不是。从URL openStream()方法返回的InputStream中读取。

如果您想知道为什么名为“ getFile”的方法实际上没有返回文件,则原因是java.net.URL是一个非常老的类。它出现在90年代中期的Java 1.0中。那时,大多数URL实际上都指向物理文件,尤其是ftp:URL,它仍然比http:URL 普遍得多。该java.net.URI中的类是更新,使用更准确的术语。(每个URL也是一个URI。)