0

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"))

I searched and I found:

analyze jar file programmatically

Extract source code from .jar file

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

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

How to search for a string in JAR files

Community
  • 1
  • 1
D.Dreko
  • 15
  • 3
  • This may be a duplicate of a duplicate . See here http://stackoverflow.com/questions/1463192/reading-content-of-a-jar-file-at-runtime and http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file . – Anton Codes May 14 '17 at 17:19
  • Possible duplicate of [How do I list the files inside a JAR file?](http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file) – Maurice Meyer May 14 '17 at 18:42

1 Answers1

0

Something along these lines:

  • open the jar with a JarInputStream
  • for each JarEntry do a Class.forName to load the class (compose the package correctly)
  • use reflection to get the method names (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.). If the RegExp part should be slow, create a regexp Pattern instance and reuse it.

A quick (Linux) alternative, for classes names only, is this:

jar tvf file.jar | grep ClassName

lorenzo
  • 556
  • 5
  • 13
  • Thanks ! Works perfectly ! <3 And, last question: How can I get all jar files in a folder (for example "C:/Users/User/AppData/Roaming/.minecraft/versions") and to analyze them ? :) – D.Dreko May 14 '17 at 21:01
  • File.listFiles(...) is the method you are looking for. If you want to include subdirectories too look at Apache commons.io FileUtils.iterateFilesAndDirs – lorenzo May 14 '17 at 22:58