If you want to access resources in a JAR-file or other resource (added with "Edit project") from a Java agent, you can not use the following code:
URL resourceURL = this.getClass().getResource(resource);
logger.debug("resourceURL: " + resourceURL);
The getResource() method always return null.
Instead, you have to use the getResourceAsStream() like this:
String resource = "/org/apache/log4j/xml/log4j.dtd";
InputStream is = this.getClass().getResourceAsStream(resource);
Reader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer buf = new StringBuffer();
int ch;
while ((ch = reader.read()) > -1) {
buf.append((char) ch);
}
reader.close();
System.out.println("buf: " + buf.toString());
To get a Java source from an agent, you just have to replace the resource String above. I.e. if you want the source of your class net.kanngard.util.logger.LoggerPrintWriter, you can write:
String resource = "net/kanngard/util/logging/LoggerPrintWriter.java"
InputStream is = this.getClass().getResourceAsStream(resource);
Reader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer buf = new StringBuffer();
int ch;
while ((ch = reader.read()) > -1) {
buf.append((char) ch);
}
reader.close();
System.out.println("buf: " + buf.toString());
Why would you want to use the source of a class? Well, if you want to export the source automatically and do some checks of the coding style etc. I will write more about this later on.