gpt4 book ai didi

java - 如何从 java jar 文件打印类和接口(interface)名称

转载 作者:行者123 更新时间:2023-12-02 03:09:04 25 4
gpt4 key购买 nike

我有一个java Jar,其中包含2个类和1个接口(interface)。我如何从 jar 中获取接口(interface)和类名。目前我可以获取类名称,但不能获取接口(interface)名称。

   List jClasses = getClasseNames("D://Test.jar");

System.out.println(jClasses.size());

for (int i = 0; i < jClasses.size(); i++) {

System.out.println("Print Classes ::" + jClasses.get(i));

if(( null != jClasses.getClass().getInterfaces()[i])) {
System.out.println(jClasses.getClass().getInterfaces()[i]);
} else {
System.out.println("No connection");
}
}

public static List getClasseNames(String jarName) {
ArrayList classes = new ArrayList();

try {
JarInputStream jarFile = new JarInputStream(new FileInputStream(
jarName));
JarEntry jarEntry;

while (true) {
jarEntry = jarFile.getNextJarEntry();
if (jarEntry == null) {
break;
}
if (jarEntry.getName().endsWith(".class")) {

classes.add(jarEntry.getName().replaceAll("/", "\\."));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return classes;
}

输出:

Print Classes ::com.java.testclient.PTest1.class
interface java.util.List
======
Print Classes ::com.java.testclient.ClassSpy.class
interface java.util.RandomAccess
======
Print Classes ::com.java.testclient.myInt.class
interface java.lang.Cloneable
======
Print Classes ::com.java.testclient.PTest.class
interface java.io.Serializable

请提出建议。

最佳答案

您可以使用此类:

package io.github.gabrielbb.java.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

/**
* @author Gabriel Basilio Brito
* @since 12/26/2016
* @version 1.1
*/
public class ClassesAndInterfacesFromJar {

public static List<Class> getJarClasses(String jarPath) throws IOException, ClassNotFoundException {
File jarFile = new File(jarPath);
return getJarClasses(jarFile);
}

public static List<Class> getJarClasses(File jar) throws IOException, ClassNotFoundException {
ArrayList<Class> classes = new ArrayList();
JarInputStream jarInputStream = null;
URLClassLoader cl;

try {
cl = URLClassLoader.newInstance(new URL[]{new URL("jar:file:" + jar + "!/")}); // To load classes inside the jar, after getting their names

jarInputStream = new JarInputStream(new FileInputStream(
jar)); // Getting a JarInputStream to iterate through the Jar files

JarEntry jarEntry = jarInputStream.getNextJarEntry();

while (jarEntry != null) {
if (jarEntry.getName().endsWith(".class")) { // Avoiding non ".class" files
String className = jarEntry.getName().replaceAll("/", "\\."); // The ClassLoader works with "." instead of "/"
className = className.substring(0, jarEntry.getName().length() - 6); // Removing ".class" from the string
Class clazz = cl.loadClass(className); // Loading the class by its name
classes.add(clazz);
}

jarEntry = jarInputStream.getNextJarEntry(); // Next File
}
} finally {
if (jarInputStream != null) {
jarInputStream.close(); // Closes the FileInputStream
}
}
return classes;
}

// Main Method for testing purposes
public static void main(String[] args) {
try {
String jarPath = "C://Test.jar";
List<Class> classes = getJarClasses(jarPath);

for (Class c : classes) {
// Here we can use the "isInterface" method to differentiate an Interface from a Class
System.out.println(c.isInterface() ? "Interface: " + c.getName() : "Class: " + c.getName());
}
} catch (Exception ex) {
System.err.println(ex);
}
}

可以在以下位置找到:

https://github.com/GabrielBB/Java-Utilities/blob/master/ClassesAndInterfacesFromJar.java

关于java - 如何从 java jar 文件打印类和接口(interface)名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41334032/

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