-2

I'm doing some hotcoding thing, and I need to use android.jar in the process. However I have to manually add android.jar to javassist (a bytecode manipulation tool) classpool, but I dont want to use the absolute path of android.jar like now,

pool.insertClassPath("/Users/xinmei/Library/Android/sdk/platforms/android-
26/android.jar")

but I dont know how to get the relative path of it, can anyone give me some hint? Thanks!

Pieter
  • 895
  • 11
  • 22
Bob975
  • 7
  • 5
  • https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – Sam Jan 29 '18 at 09:21

1 Answers1

0

This is a method that I use copied from one of my projects:

public static String getPathName() {
    String fullPath = "";
    int endOfPath = 0;
    try {
        fullPath = new File(JOTI_App.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).toString();
        endOfPath = fullPath.lastIndexOf(File.separatorChar);
    } catch (URISyntaxException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return fullPath.substring(0, endOfPath);
}
AndyW
  • 410
  • 5
  • 17
  • My fault, the website editing tools messed up my code and I didn't check. I will edit my response. – AndyW Jan 29 '18 at 10:23
  • thank you, btw I'm working on a groovy project, it's actually a plugin, does this matter? – Bob975 Jan 29 '18 at 10:37
  • And I'm not actually using any contents that's in the jar, I just need to add the path of the jar to javassist classPool – Bob975 Jan 29 '18 at 10:39
  • This should return the path of the named class (in this case "JOTI_App") so you can do with it whatever you want. – AndyW Jan 29 '18 at 10:52
  • This is where I dont quite understand... How is this relate to the Jar file I want? and as it's known android.jar is a system jar, in which class am I gonna search it for? – Bob975 Jan 29 '18 at 10:56