如何在jar文件中获取类的名称?

时间:2022-04-08 20:23:23

I have a JAR file and I need to get the name of all classes inside this JAR file. How can I do that?

我有一个JAR文件,我需要得到这个JAR文件中所有类的名称。我怎么做呢?

I googled it and saw something about JarFile or Java ClassLoader but I have no idea how to do it.

我在谷歌上搜索了一下,发现了一些关于JarFile或Java ClassLoader的东西,但我不知道该怎么做。

9 个解决方案

#1


57  

Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the .class files, and then determine which Java class each .class file represents; or (b) you can use a library that does this for you.

不幸的是,Java没有提供在“本机”JRE中列出类的简单方法。这给您留下了几个选项:(a)对于任何给定的JAR文件,您可以列出JAR文件中的条目,找到.class文件,然后确定每个.class文件代表的是哪个Java类;或者(b)你可以使用一个为你做这件事的库。

Option (a): Scanning JAR files manually

In this option, we'll fill classNames with the list of all Java classes contained inside a jar file at /path/to/jar/file.jar.

在这个选项中,我们将在/path/to/jar/file.jar的jar文件中填入包含所有Java类的列表。

List<String> classNames = new ArrayList<String>();
ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
    if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
        // This ZipEntry represents a class. Now, what class does it represent?
        String className = entry.getName().replace('/', '.'); // including ".class"
        classNames.add(className.substring(0, className.length() - ".class".length()));
    }
}

Option (b): Using specialized reflections libraries

Guava

Guava has had ClassPath since at least 14.0, which I have used and liked. One nice thing about ClassPath is that it doesn't load the classes it finds, which is important when you're scanning for a large number of classes.

番石榴的类路径至少从14.0开始就有了,我用过,也很喜欢。类路径的一个好处是它不加载它找到的类,这在扫描大量类时非常重要。

ClassPath cp=ClassPath.from(Thread.currentThread().getContextClassLoader());
for(ClassPath.ClassInfo info : cp.getTopLevelClassesRecurusive("my.package.name")) {
    // Do stuff with classes here...
}

Reflections

I haven't personally used the Reflections library, but it seems well-liked. Some great examples are provided on the website like this quick way to load all the classes in a package provided by any JAR file, which may also be useful for your application.

我个人还没有使用过反射库,但它似乎很受欢迎。网站上提供了一些很好的例子,比如这种快速加载任何JAR文件提供的包中的所有类的方法,这对您的应用程序可能也很有用。

Reflections reflections = new Reflections("my.project.prefix");

Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);

Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

#2


137  

You can use Java jar tool. List the content of jar file in a txt file and you can see all the classes in the jar.

您可以使用Java jar工具。列出txt文件中的jar文件的内容,您可以看到jar中的所有类。

jar tvf jarfile.jar

#3


17  

Maybe you are looking for jar command to get the list of classes in terminal,

也许您正在寻找jar命令来获取终端中的类列表,

$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar 
META-INF/
META-INF/MANIFEST.MF
org/
org/apache/
org/apache/spark/
org/apache/spark/unused/
org/apache/spark/unused/UnusedStubClass.class
META-INF/maven/
META-INF/maven/org.spark-project.spark/
META-INF/maven/org.spark-project.spark/unused/
META-INF/maven/org.spark-project.spark/unused/pom.xml
META-INF/maven/org.spark-project.spark/unused/pom.properties
META-INF/NOTICE

where,

在那里,

-t  list table of contents for archive
-f  specify archive file name

Or, just grep above result to see .classes only

或者,只看到上面的grep就可以看到。类。

$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar | grep .class
org/apache/spark/unused/UnusedStubClass.class

To see number of classes,

要查看类的数量,

jar tvf launcher/target/usergrid-launcher-1.0-SNAPSHOT.jar | grep .class | wc -l
61079

#4


8  

This is a hack I'm using:

这是我使用的一个技巧:

You can use java's autocomplete like this:

你可以像这样使用java的自动完成:

java -cp path_to.jar <Tab>

This will give you a list of classes available to pass as the starting class. Of course, trying to use one that has no main file will not do anything, but you can see what java thinks the classes inside the .jar are called.

这将为您提供一个可用的类列表,以作为开始类传递。当然,尝试使用一个没有主文件的类不会做任何事情,但是您可以看到java认为在.jar中调用的类是什么。

#5


5  

Below command will list the content of a jar file.

下面的命令将列出jar文件的内容。

command :- unzip -l jarfilename.jar.

命令:-解压-l jarame. jar。

sample o/p :-

样本o / p:-

Archive: hello-world.jar Length Date Time Name --------- ---------- ----- ---- 43161 10-18-2017 15:44 hello-world/com/ami/so/search/So.class 20531 10-18-2017 15:44 hello-world/com/ami/so/util/SoUtil.class --------- ------- 63692 2 files

存档:hello world。jar日期时间长度的名字- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 43161 10-18-2017 44 hello world /com/ami/so/search/So。第20531课10-18-2017 15:44 hello-world/com/ami/so/util/SoUtil。类--------- -- 63692 2个文件

According to manual of unzip

根据解压缩手册

-l list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are printed, along with totals for all files specified. If UnZip was compiled with OS2_EAS defined, the -l option also lists columns for the sizes of stored OS/2 extended attributes (EAs) and OS/2 access control lists (ACLs). In addition, the zipfile comment and individual file comments (if any) are displayed. If a file was archived from a single-case file system (for example, the old MS-DOS FAT file system) and the -L option was given, the filename is converted to lowercase and is prefixed with a caret (^).

-l列出归档文件(短格式)。打印指定文件的名称、未压缩文件大小、修改日期和时间,以及指定的所有文件的总数。如果UnZip是用OS2_EAS定义的,那么-l选项还列出了存储的OS/2扩展属性(EAs)和OS/2访问控制列表(acl)大小的列。此外,还会显示zipfile注释和单独的文件注释(如果有的话)。如果一个文件从一个单一大小写的存档文件系统(例如,旧的ms - dos FAT文件系统)和- l选项,文件名是转换为小写,前缀与插入符号(^)。

#6


3  

You can try:

你可以尝试:

jar tvf jarfile.jar 

This will be helpful only if your jar is executable i.e. in manifest you have defined some class as main class

只有当您的jar是可执行的时,这才会有帮助。例如,在manifest中,您已经定义了一些类作为主类

#7


3  

You can use the

您可以使用

jar tf example.jar

#8


2  

You can try this :

你可以试试这个:

unzip -v /your/jar.jar

This will be helpful only if your jar is executable i.e. in manifest you have defined some class as main class

只有当您的jar是可执行的时,这才会有帮助。例如,在manifest中,您已经定义了一些类作为主类

#9


0  

Description OF Solution : Eclipse IDE can be used for this by creating a sample java project and add all jars in the Project Build path

解决方案的描述:可以通过创建一个示例java项目并在项目构建路径中添加所有jar来使用Eclipse IDE

STEPS below:

下面的步骤:

  1. Create a sample Eclipse Java project.

    创建一个示例Eclipse Java项目。

  2. All all the jars you have in its Build Path

    您在构建路径中拥有的所有jar

  3. CTRL+SHIFT+T and Type the full class name .

    按CTRL+SHIFT+T键,输入完整的类名。

  4. Results will be displayed in the window with all the jars having that class. See attached picture . 如何在jar文件中获取类的名称?

    结果将显示在具有该类的所有jar的窗口中。请看附呈图片。

#1


57  

Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the .class files, and then determine which Java class each .class file represents; or (b) you can use a library that does this for you.

不幸的是,Java没有提供在“本机”JRE中列出类的简单方法。这给您留下了几个选项:(a)对于任何给定的JAR文件,您可以列出JAR文件中的条目,找到.class文件,然后确定每个.class文件代表的是哪个Java类;或者(b)你可以使用一个为你做这件事的库。

Option (a): Scanning JAR files manually

In this option, we'll fill classNames with the list of all Java classes contained inside a jar file at /path/to/jar/file.jar.

在这个选项中,我们将在/path/to/jar/file.jar的jar文件中填入包含所有Java类的列表。

List<String> classNames = new ArrayList<String>();
ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
    if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
        // This ZipEntry represents a class. Now, what class does it represent?
        String className = entry.getName().replace('/', '.'); // including ".class"
        classNames.add(className.substring(0, className.length() - ".class".length()));
    }
}

Option (b): Using specialized reflections libraries

Guava

Guava has had ClassPath since at least 14.0, which I have used and liked. One nice thing about ClassPath is that it doesn't load the classes it finds, which is important when you're scanning for a large number of classes.

番石榴的类路径至少从14.0开始就有了,我用过,也很喜欢。类路径的一个好处是它不加载它找到的类,这在扫描大量类时非常重要。

ClassPath cp=ClassPath.from(Thread.currentThread().getContextClassLoader());
for(ClassPath.ClassInfo info : cp.getTopLevelClassesRecurusive("my.package.name")) {
    // Do stuff with classes here...
}

Reflections

I haven't personally used the Reflections library, but it seems well-liked. Some great examples are provided on the website like this quick way to load all the classes in a package provided by any JAR file, which may also be useful for your application.

我个人还没有使用过反射库,但它似乎很受欢迎。网站上提供了一些很好的例子,比如这种快速加载任何JAR文件提供的包中的所有类的方法,这对您的应用程序可能也很有用。

Reflections reflections = new Reflections("my.project.prefix");

Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);

Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

#2


137  

You can use Java jar tool. List the content of jar file in a txt file and you can see all the classes in the jar.

您可以使用Java jar工具。列出txt文件中的jar文件的内容,您可以看到jar中的所有类。

jar tvf jarfile.jar

#3


17  

Maybe you are looking for jar command to get the list of classes in terminal,

也许您正在寻找jar命令来获取终端中的类列表,

$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar 
META-INF/
META-INF/MANIFEST.MF
org/
org/apache/
org/apache/spark/
org/apache/spark/unused/
org/apache/spark/unused/UnusedStubClass.class
META-INF/maven/
META-INF/maven/org.spark-project.spark/
META-INF/maven/org.spark-project.spark/unused/
META-INF/maven/org.spark-project.spark/unused/pom.xml
META-INF/maven/org.spark-project.spark/unused/pom.properties
META-INF/NOTICE

where,

在那里,

-t  list table of contents for archive
-f  specify archive file name

Or, just grep above result to see .classes only

或者,只看到上面的grep就可以看到。类。

$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar | grep .class
org/apache/spark/unused/UnusedStubClass.class

To see number of classes,

要查看类的数量,

jar tvf launcher/target/usergrid-launcher-1.0-SNAPSHOT.jar | grep .class | wc -l
61079

#4


8  

This is a hack I'm using:

这是我使用的一个技巧:

You can use java's autocomplete like this:

你可以像这样使用java的自动完成:

java -cp path_to.jar <Tab>

This will give you a list of classes available to pass as the starting class. Of course, trying to use one that has no main file will not do anything, but you can see what java thinks the classes inside the .jar are called.

这将为您提供一个可用的类列表,以作为开始类传递。当然,尝试使用一个没有主文件的类不会做任何事情,但是您可以看到java认为在.jar中调用的类是什么。

#5


5  

Below command will list the content of a jar file.

下面的命令将列出jar文件的内容。

command :- unzip -l jarfilename.jar.

命令:-解压-l jarame. jar。

sample o/p :-

样本o / p:-

Archive: hello-world.jar Length Date Time Name --------- ---------- ----- ---- 43161 10-18-2017 15:44 hello-world/com/ami/so/search/So.class 20531 10-18-2017 15:44 hello-world/com/ami/so/util/SoUtil.class --------- ------- 63692 2 files

存档:hello world。jar日期时间长度的名字- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 43161 10-18-2017 44 hello world /com/ami/so/search/So。第20531课10-18-2017 15:44 hello-world/com/ami/so/util/SoUtil。类--------- -- 63692 2个文件

According to manual of unzip

根据解压缩手册

-l list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are printed, along with totals for all files specified. If UnZip was compiled with OS2_EAS defined, the -l option also lists columns for the sizes of stored OS/2 extended attributes (EAs) and OS/2 access control lists (ACLs). In addition, the zipfile comment and individual file comments (if any) are displayed. If a file was archived from a single-case file system (for example, the old MS-DOS FAT file system) and the -L option was given, the filename is converted to lowercase and is prefixed with a caret (^).

-l列出归档文件(短格式)。打印指定文件的名称、未压缩文件大小、修改日期和时间,以及指定的所有文件的总数。如果UnZip是用OS2_EAS定义的,那么-l选项还列出了存储的OS/2扩展属性(EAs)和OS/2访问控制列表(acl)大小的列。此外,还会显示zipfile注释和单独的文件注释(如果有的话)。如果一个文件从一个单一大小写的存档文件系统(例如,旧的ms - dos FAT文件系统)和- l选项,文件名是转换为小写,前缀与插入符号(^)。

#6


3  

You can try:

你可以尝试:

jar tvf jarfile.jar 

This will be helpful only if your jar is executable i.e. in manifest you have defined some class as main class

只有当您的jar是可执行的时,这才会有帮助。例如,在manifest中,您已经定义了一些类作为主类

#7


3  

You can use the

您可以使用

jar tf example.jar

#8


2  

You can try this :

你可以试试这个:

unzip -v /your/jar.jar

This will be helpful only if your jar is executable i.e. in manifest you have defined some class as main class

只有当您的jar是可执行的时,这才会有帮助。例如,在manifest中,您已经定义了一些类作为主类

#9


0  

Description OF Solution : Eclipse IDE can be used for this by creating a sample java project and add all jars in the Project Build path

解决方案的描述:可以通过创建一个示例java项目并在项目构建路径中添加所有jar来使用Eclipse IDE

STEPS below:

下面的步骤:

  1. Create a sample Eclipse Java project.

    创建一个示例Eclipse Java项目。

  2. All all the jars you have in its Build Path

    您在构建路径中拥有的所有jar

  3. CTRL+SHIFT+T and Type the full class name .

    按CTRL+SHIFT+T键,输入完整的类名。

  4. Results will be displayed in the window with all the jars having that class. See attached picture . 如何在jar文件中获取类的名称?

    结果将显示在具有该类的所有jar的窗口中。请看附呈图片。