我有一个 Swing 应用程序,我正试图将其打包到一个可运行的 JAR 文件中。它的部分 DAO 功能是以 CSV 格式读取和写入 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)
从命令行。这是通过 mvn package
和 maven-assembly-plugin
规范构建的 jar
<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>
我有一个 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();
}
}
}
我只在执行的时候出现这个错误
java -jar dictionary-jar-with-dependencies.jar
在 /Users/jason/projects/test-dict/target/
中。如果我通过 IDE 运行,我不会收到任何错误。
事情是,我可以通过 vim dictionary-jar-with-dependencies.jar
在 jar 的末尾看到 dictData.dat
:
...
...
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
那么,如果不将数据文件移动到 test-dict/com/text/dictionary/data
位置,我该如何解决这个问题?
最佳答案
您不能将 classLoader.getResource(DICTIONARYFILE)
返回的 URL 转换为文件名,因为应用程序资源通常不是实际文件。即使可以,URL.getFile()
也是错误的做法。改用这个:
URL dataFile = classLoader.getResource(DICTIONARYFILE);
dataReader = new InputStreamReader(dataFile.openStream());
详细解释:
当您的程序从 .jar 文件运行时(几乎所有程序都这样做,除了在某些开发环境中),Class.getResource 和 ClassLoader.getResource 方法返回一个 URL,它是一个 jar URL。 jar URL 是具有以下格式的特定于 Java 的 URL 方案:
jar:
url-of-jarfile!
path-of-jar-entry
在您的例子中,.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
。
您请求 /dictData.dat
的 jar 文件中的条目。因此,您的 getResource
调用返回的 URL 是:
jar:file:/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 是一个非常古老的类。它出现在 Java 1.0 中,早在 90 年代中期。那时,大多数 URL 实际上确实指向物理文件,尤其是 ftp:
URL,它仍然比 http:
URL 更常见。 java.net.URI class 更新并且使用更准确的术语。 (每个 URL 也是一个 URI。)
关于java - 未找到文件异常 - 从 jar 执行时资源文件夹中的数据文件抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390088/