从 JAR 中读取文件(NoSuchFileException)

Reading Files from JAR (NoSuchFileException)

我正在尝试从 JAR 文件中读取一些文件。 JAR 文件位于 Tomcat 的 lib 文件夹中。我正在阅读这样的文件:

String filePath = DirectoryReader.class.getClassLoader().getResource(directoryPath).getPath();

System.out.println("Directory Path: " + directoryPath);
System.out.println("File Path 1: " + filePath);

filePath = filePath.substring(6, filePath.indexOf("!"));

System.out.println("File Path 2: " + filePath);

Path configJARLocation = Paths.get(filePath);
System.out.println("JAR location: " + configJARLocation.toString());
InputStream inputStream = Files.newInputStream(configJARLocation);
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
InputStreamReader inputStreamReader = null;

但我在日志中发现异常:

Directory Path: META-INF/gmm/xmlFiles/predefined/ipsec/
File Path 1: file:/opt/app/tools/tomcat/lib/appConfigurations.jar!/META-INF/gmm/xmlFiles/predefined/ipsec/
File Path 2: opt/app/tools/tomcat/lib/appConfigurations.jar
JAR location: opt/app/tools/tomcat/lib/appConfigurations.jar
java.nio.file.NoSuchFileException: opt/app/tools/tomcat/lib/appConfigurations.jar
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
        at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
        at java.nio.file.Files.newByteChannel(Files.java:361)
        at java.nio.file.Files.newByteChannel(Files.java:407)
        at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
        at java.nio.file.Files.newInputStream(Files.java:152)
        at com.gtc.logicalprovisioning.dac.file.DirectoryReader.process(DirectoryReader.java:72)

除了opt前面的前导/,我看不出有什么不对的地方。代码的第72行是:

InputStream inputStream = Files.newInputStream(configJARLocation);

相同的代码在 Windows 中有效。只有当我在我的 Unix 环境中部署时,它才开始失败。我最初认为这是权限或访问问题。但似乎并非如此。我先将 chmod 从 755 更改为 JAR 文件的 777。然后我使用 chown 更改了权限。但它仍然没有用。我不是 Unix 专家,所以我不能深入研究这个问题。好心提醒!谢谢!

As I said above in my comment.The problem was the file:/ prefix. Since I was doing string manipulation, with the start index of 6, it was causing the first / to get cut from the Unix path.

Windows Path is like:

file:/H:/...

Can't really say why the Windows path has an extra / in front. If anyone can enlighten me on it, that would be nice.

Unix Path is like:

file:/opt/...

So the cleaner option would be the use the URL/URI API to get it done. Should have thought of this earlier, as it is a more graceful way of tackling this.

// JAR File Reading
String filePath = DirectoryReader.class.getClassLoader().getResource(directoryPath).getPath();

filePath = filePath.substring(0, filePath.indexOf("!"));

URL url = new URL(filePath);

Path configJARLocation = Paths.get(url.toURI());

InputStream inputStream = Files.newInputStream(configJARLocation);
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
InputStreamReader inputStreamReader = null;

This worked for me.

改变你的

String filePath = DirectoryReader.class.getClassLoader().getResource(directoryPath).getPath();

这样

String filePath = getClass().getClassLoader().getResource(fileName).getFile();