0

I am decompiling apks and analyzing their source code using the AST Parser (not working in Eclipse environment). To create the bindings, I need to setEnvironment(), but I don't know what to put as the class path. Would it be the same for each set of source code? What would I use as the class path string for a given project?

Erin
  • 29
  • 1
  • 8
  • I think I found the answers you were looking for. I think the majority of JAVA classes are defined in the rt.jar file. So you would need that in your classpath, next to your own java files. Take a look down, I think my solution should help you out. – Jernej K Mar 12 '16 at 16:06

1 Answers1

0

Seems like you would have to include the classpath of all the java files you use - by that I mean the jar and your own java files. I do not know if this is your actual problem, because you would have to give more information about it. But in case you are just trying to figure out what goes into the method, I found an example here.

And here the cut out example from the link I posted up:

    String path ="C:\\Users\\pc\\workspace\\asttester\\src\\test\\Apple.java";
    File file = new File(path);
    String str = Util.readFileToString(file);

    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setResolveBindings(true);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    parser.setBindingsRecovery(true);

    Map options = JavaCore.getOptions();
    parser.setCompilerOptions(options);

    String unitName = "Apple.java";
    parser.setUnitName(unitName);

    String[] sources = { "C:\\Users\\pc\\workspace\\asttester\\src" }; 
    String[] classpath = {"C:\\Program Files\\Java\\jre1.8.0_25\\lib\\rt.jar"};

    // sources is the source dir of all your java files
    // classpath is the java jar containing all the needed classes
    parser.setEnvironment(classpath, sources, new String[] { "UTF-8"}, true);
    parser.setSource(str.toCharArray());

    CompilationUnit cu = (CompilationUnit) parser.createAST(null);

As you can see from the example above, the method definition is:

public void setEnvironment(String[] classpathEntries,
                       String[] sourcepathEntries,
                       String[] encodings,
                       boolean includeRunningVMBootclasspath)

So, like I said, you need to add all java jars you need at compile time. Otherwise it will not compile java classes to .class files. So you have to verify all your JAVA code you are using is contained in one of the jars you are adding to the classpath. As you now, per default, eclipse always add all JAVA jars to the classpath, when you create a JAVA project.

And to determine where the JAVA file is, you can take a look at this code I found:

Class klass = String.class;
URL location = klass.getResource('/'+klass.getName().replace('.', '/')+".class");

You can get more information on the last one here.

Community
  • 1
  • 1
Jernej K
  • 1,602
  • 2
  • 25
  • 38
  • Thank you so much for all of your thought into the answer. When you say "all of the Java code you are using", do you mean the source code I'm analyzing? I'm given the code at runtime, the user inputs the path, so I'm not sure how I would get the jars at compile time, or even get the jars in the first place. – Erin Mar 12 '16 at 21:27
  • What meant was that you need to verify that java classes like java.util.List (which is part of rt.jar if I am not mistaken) have the right jar included in the classpath. That's what I meant with java files. If you would be using the List class, then you would need to have rt.jar in the classpath. Each jar contains different java class files. – Jernej K Mar 12 '16 at 23:05
  • And the example here is only to show how to set environment, like you asked. – Jernej K Mar 12 '16 at 23:12
  • I placed the source code into Android Studio and compiled it. I then took each of the jar files and added them to the class path string array needed for setEnvironment. Resolve bindings claims to be true, but the type for my method parameters comes up as "Missing". Do you have any thoughts? I would have thought those jar files would be sufficient since I was able to compile. Am I missing something else? – Erin Mar 13 '16 at 03:32
  • Resolved - I was missing the android.jar for the sdk. Thank you very much! – Erin Mar 13 '16 at 03:41
  • I am glad you have a solution and that the code works. – Jernej K Mar 13 '16 at 03:50