简体   繁体   English

分析并获取jar文件的代码/字符串-Java

[英]Analyze and get code/string of a jar file - Java

I want to analyze the code (or strings) of a jar file (example: if a jar file contains "cheese"), I searched a lot but I don't know how to do this :/ I don't want to use JD-GUI because my program have to be automatic (user press a button and then he have to know if a jar file contains a specific method (for example "public void onEnable()" or a string (for example "cheese")) 我想分析jar文件的代码(或字符串)(例如:如果jar文件包含“奶酪”),我进行了很多搜索,但我不知道该怎么做:/我不想使用JD-GUI,因为我的程序必须是自动的(用户按下按钮,然后他必须知道jar文件是否包含特定方法(例如“ public void onEnable()”或字符串(例如“ cheese”)))

I searched and I found: 我搜索后发现:

analyze jar file programmatically 以编程方式分析jar文件

Extract source code from .jar file 从.jar文件中提取源代码

https://tomassetti.me/getting-started-with-javaparser-analyzing-java-code-programmatically/ https://tomassetti.me/getting-started-with-javaparser-analyzing-java-code-programmatically/

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html

How to search for a string in JAR files 如何在JAR文件中搜索字符串

Something along these lines: 遵循以下原则:

  • open the jar with a JarInputStream JarInputStream打开罐子
  • for each JarEntry do a Class.forName to load the class (compose the package correctly) 对于每个JarEntry,请执行Class.forName加载类(正确编写包)
  • use reflection to get the method names ( Class.getMethods ) 使用反射获取方法名称( Class.getMethods
  • add the names, and required attributes, to a List to do the searches. 将名称和必需的属性添加到列表中以进行搜索。 Unless you are using huge jars or very old hardware you can do a simple full scan of the list of names using a contains() or matches() , probably you do not need fancy indexing tools (Sphinx, Lucene, etc.). 除非您使用大罐子或非常旧的硬件,否则可以使用contains()或matches()对名称列表进行简单的完整扫描,可能不需要花哨的索引工具(Sphinx,Lucene等)。 If the RegExp part should be slow, create a regexp Pattern instance and reuse it. 如果RegExp部分应该很慢,请创建一个regexp Pattern实例并重新使用它。

A quick (Linux) alternative, for classes names only, is this: 仅针对类名称的一种快速(Linux)替代方法是:

jar tvf file.jar | jar tvf file.jar | grep ClassName grep类别名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM