获取嵌套 .jar 中的 .class 文件
我需要获取位于 .jar 文件中的所有 .class 文件,而该文件又位于另一个 .ear 文件中。
我正在尝试使用 JarInputStream 来访问 .ear。这工作正常。但是,当我迭代 JarInputStream 内的 JarEntry 元素时,我似乎无法将其作为 Jar 文件打开以读取其中的任何 .class 元素。
JarInputStream jarFile = new JarInputStream(new FileInputStream("c:/path/to/my/.ear"));
JarEntry jarEntry = null;
while(true) {
jarEntry = jarFile.getNextJarEntry();
if(jarEntry == null) {
break;
}
if((jarEntry.getName().endsWith(".jar"))) {
//Access to the nested jar?
}
}
编辑:值得一提的是,上面的代码与我试图以编程方式查找的类位于同一个 jar 中。
I need to get all the .class files located in a .jar file which in turn is located within another .ear file.
I am trying to use the JarInputStream to get access to the .ear. This is working ok. However, when I iterate the JarEntry elements inside the JarInputStream, I can't seem to be able to open it as a Jar file in order to read any .class elements within it.
JarInputStream jarFile = new JarInputStream(new FileInputStream("c:/path/to/my/.ear"));
JarEntry jarEntry = null;
while(true) {
jarEntry = jarFile.getNextJarEntry();
if(jarEntry == null) {
break;
}
if((jarEntry.getName().endsWith(".jar"))) {
//Access to the nested jar?
}
}
Edited: worth mentioning that the code above is in the same jar as the classes I am trying to find programmatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该首先从 .ear 文件中提取 .jar 文件,然后尝试读取类文件。
事实上,应用程序服务器也是这样做的。他们将 EAR 文件提取到临时目录中,然后从那里加载类。
You should first extract the .jar file from the .ear file and then try to read the class file.
In fact, that's how even the Application Servers do. They extract the EAR file into a temporary directory and then load the classes from there.
你尝试过这个吗?
Did you try this?