gpt4 book ai didi

java - 访问 JAR 文件时出现 NotDirectoryException

转载 作者:行者123 更新时间:2023-12-01 09:59:42 25 4
gpt4 key购买 nike

晚上好,

我正在尝试动态访问包含几个 *.class 文件的 JAR 文件。我读过相关内容,一个好方法是使用 java 类 FileSystem 来访问 jar 内的多个文件。我编写了以下代码:

private void loadJar(String gameName) {
final String packageBinaryName = gameName;
final String pathname = packageBinaryName.replace(".", "/") +".jar";
URI uri = null;
uri = new File(pathname).toURI();//.toURL();
final String[] array = uri.toString().split("!");
FileSystem fs = null;
Path dir;

try
{
if(array.length == 2)
{
final Map<String, String> env = new HashMap<>();
fs = FileSystems.newFileSystem(URI.create(array[0]), env);
dir = fs.getPath(array[1]);
}
else
dir = Paths.get(uri);

DirectoryStream<Path> stream;

System.out.println(dir);
stream = Files.newDirectoryStream(dir, "*.class");
}
catch (IOException e)
{
e.printStackTrace();
}
}

但是我得到了 java.nio.file.NotDirectoryException

JAR 文件存在并且它指向写入路径。可能是什么导致了这个问题?

目录打印以下内容:

C:\Users\afonso\workspace\GameEvaluatorDummy\src\TicTacToe.jar

编辑:请注意,调试 dir 语句会在 else 语句上获取其值

dir=Paths.get(uri);

最佳答案

当然会到达else路径。
您正在检查的 URI 绝不会包含感叹号。
但只是 JAR 文件的 URI。
除非你给这个方法一些带感叹号的参数,但我想说这也行不通。

你所追求的是

FileSystem fs = FileSystems.newFileSystem(new URI("jar:file:///path/to/the.jar"), Collections.<String,?>emptyMap());
Path dir = fs.getPath("/");
Files.newDirectoryStream(dir, "*.class*);

但请注意,这不会自动递归到子目录中。

关于java - 访问 JAR 文件时出现 NotDirectoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36901262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com