将 URL 转换为普通的 windows 文件名 Java 6
Convert URL to normal windows filename Java 6
我正在尝试从 jar 文件中读取包名。我的问题是,当我得到 URL 时,它包含无法识别的形式,需要被 windows 文件识别。
我阅读了这个解决方案。但这对我没有帮助。 Convert URL to normal windows filename Java.
directoryURL.toURI().getSchemeSpecificPart()
不转换 windows 样式。
这是我的代码。
// Get a File object for the package
URL directoryURL = Thread.currentThread().getContextClassLoader()
.getResource(packageNameSlashed);
logger.info("URI" + directoryURL.toURI());
logger.info("Windows file Name" + directoryURL.toURI().getSchemeSpecificPart());
// build jar file name, then loop through zipped entries
jarFileName = URLDecoder.decode(directoryURL.getFile(), "UTF-8");
jarFileName = jarFileName.substring(0, jarFileName.indexOf(".jar"));
// HERE Throws exception"
jf = new JarFile(jarFileName + ".jar");
while (jarEntries.hasMoreElements()) {
entryName = jarEntries.nextElement().getName();
logger.info("Entry name: " + entryName);
if (entryName.startsWith(packageNameSlashed)
&& entryName.length() > packageNameSlashed.length() + 5
&& entryName.endsWith(".class")) {
entryName = entryName.substring(packageNameSlashed.length() + 1);
packageClassNames.put(entryName, packageName);
}
}
这是日志。
16-02-2015 14:02:15 INFO - URI jar:file:/C:/SVN/AAA/trunk/aaa/client/target/server-1.0.jar!/packageName
16-02-2015 14:02:15 INFO Windows file Name file:/C:/SVN/AAA/trunk/aaa/client/target/server-1.0.jar!/packageName
A "jar:..." URL 没有识别文件。相反,它标识 JAR 文件的成员。
语法是(粗略地说)"jar:<jar-url>!<path-within-jar>"
,其中 the 本身就是 URL;例如a "file:" URL 在你的例子中。
如果您要打开 JAR 文件并重复这样的条目,您需要:
提取原URL
的schemeSpecificPart
在“!”上拆分 schemeSpecificPart字符.
解析“!”之前的部分作为 URI,然后使用 File(URI)
获取 File
.
使用File
打开ZipFile
。
查找“!”后面的部分在 ZipFile
...
Stephen 的回答包含了您需要的所有要素。
- 使用 getResource(package).getURI() 或 getResoucer(package).toFile 获取资源路径。
- 对其进行子字符串提取 file:// 和 ! 之间的部分这是您感兴趣的 jar 的物理位置的路径。
- 在此子路径上新建文件,您就拥有了您的 jar 的句柄。
- jar 是普通的 zip 文件,就这样处理它(java.util.zip 网上有手册)。
- 列出您的 zip 文件的内容(现在您可能需要使用后面的位进行导航!在您的原始路径中签名),然后您将获得您的 类 名称。
我不确定这是否是实现您目标的最佳方式,我会检查 类 发现(您正在尝试做的事情)是如何在一些开源框架中实现的(例如tomcat 使用它,JPA implementation to find the entitities)。apache 上也有发现项目,但它似乎已经死了一段时间了。
我正在尝试从 jar 文件中读取包名。我的问题是,当我得到 URL 时,它包含无法识别的形式,需要被 windows 文件识别。
我阅读了这个解决方案。但这对我没有帮助。 Convert URL to normal windows filename Java.
directoryURL.toURI().getSchemeSpecificPart()
不转换 windows 样式。
这是我的代码。
// Get a File object for the package
URL directoryURL = Thread.currentThread().getContextClassLoader()
.getResource(packageNameSlashed);
logger.info("URI" + directoryURL.toURI());
logger.info("Windows file Name" + directoryURL.toURI().getSchemeSpecificPart());
// build jar file name, then loop through zipped entries
jarFileName = URLDecoder.decode(directoryURL.getFile(), "UTF-8");
jarFileName = jarFileName.substring(0, jarFileName.indexOf(".jar"));
// HERE Throws exception"
jf = new JarFile(jarFileName + ".jar");
while (jarEntries.hasMoreElements()) {
entryName = jarEntries.nextElement().getName();
logger.info("Entry name: " + entryName);
if (entryName.startsWith(packageNameSlashed)
&& entryName.length() > packageNameSlashed.length() + 5
&& entryName.endsWith(".class")) {
entryName = entryName.substring(packageNameSlashed.length() + 1);
packageClassNames.put(entryName, packageName);
}
}
这是日志。
16-02-2015 14:02:15 INFO - URI jar:file:/C:/SVN/AAA/trunk/aaa/client/target/server-1.0.jar!/packageName
16-02-2015 14:02:15 INFO Windows file Name file:/C:/SVN/AAA/trunk/aaa/client/target/server-1.0.jar!/packageName
A "jar:..." URL 没有识别文件。相反,它标识 JAR 文件的成员。
语法是(粗略地说)"jar:<jar-url>!<path-within-jar>"
,其中 the 本身就是 URL;例如a "file:" URL 在你的例子中。
如果您要打开 JAR 文件并重复这样的条目,您需要:
提取原URL
的schemeSpecificPart
在“!”上拆分 schemeSpecificPart字符.
解析“!”之前的部分作为 URI,然后使用
File(URI)
获取File
.使用
File
打开ZipFile
。查找“!”后面的部分在
ZipFile
...
Stephen 的回答包含了您需要的所有要素。
- 使用 getResource(package).getURI() 或 getResoucer(package).toFile 获取资源路径。
- 对其进行子字符串提取 file:// 和 ! 之间的部分这是您感兴趣的 jar 的物理位置的路径。
- 在此子路径上新建文件,您就拥有了您的 jar 的句柄。
- jar 是普通的 zip 文件,就这样处理它(java.util.zip 网上有手册)。
- 列出您的 zip 文件的内容(现在您可能需要使用后面的位进行导航!在您的原始路径中签名),然后您将获得您的 类 名称。
我不确定这是否是实现您目标的最佳方式,我会检查 类 发现(您正在尝试做的事情)是如何在一些开源框架中实现的(例如tomcat 使用它,JPA implementation to find the entitities)。apache 上也有发现项目,但它似乎已经死了一段时间了。