0

I'm using JarFile and JarURLConnection to load files out of a jar file. I'm then taking the classes, and loading them via BCEL (ByteCode Engineering Library, apache library). I cant just directly use a class loader because im modifying some classes slightly with the BCEL. I need to load the classes by their bytes into my bcel loader. However, one of the classes I'm loading references a resource. This resource is inside of the jar, so I can get the file (When iterating over the entries in the JarFile, I ignore the regular files, and take the class files for loading later). But just having the file won't do me any good, as the class loads it as a resource. Is there any way I can take that resource from the jar (well I can take it and load it into a byte[], the next part is the issue) and dynamically add it as a resource for my program, so that the classes that I load wont be missing their resources?

Got a lot of stuff here, if anythings confusing, ask in comments, I might've said something wrong, or missed something altogether :) Thanks

I'll show a little of my class loader here (extends ClassLoader):

@Override
public URL getResource(String name) {
    System.out.println("LOADING RESOURCE: " + name);

    try {
        return new URL(null, name, new Handler(files));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

Now, it is printing out "LOADING RESOURCE: filename", but its then giving me a MalformedURLException (I have no protocol atm, just a file path, that's not a true valid path, but it's just an attempt to give it to my Handler class below).

class Handler extends URLStreamHandler {

@Override
protected URLConnection openConnection(URL u) throws IOException {

    return new URLConnection(u) {

        @Override
        public void connect() throws IOException {

        }
        @Override
        public InputStream getInputStream() throws IOException {
            System.out.println("IS: " + url);
            return /*method to get input steam*/;
        }

    };
}

}

The /*method to get input steam*/ is set in my real code, but that's not relevant here. So any further ideas with this?

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • have you tried Classloader's getResourceAsStream()? http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String) – mazaneicha Aug 12 '12 at 05:35
  • Presumably the BCEL is using kind of classloader of its own, you need to find a way to add the resources reference to that classloader – MadProgrammer Aug 12 '12 at 06:10
  • @mazaneicha I'm not trying to get the file myself, I can do that. One of the classes that im loading needs to, and he's, I assume, using getResourceAsStream() already. – Alex Coleman Aug 12 '12 at 06:30
  • @MadProgrammer I'm using my own custom classloader class (extends ClassLoader) to load the classes. However, I'm not sure how this will help me. I see I can override getResource methods, but they return a URL, I've got a byte[] (or JarEntry if that helps), and this isn't the system class loader (not sure if there's a way to set it as such). Also, with BCEL, I can set the classes Repository, Repositry is an abstract class, and one of it's method's is getClassPath, but don't think that will help either as there is not path to the resources, just data. – Alex Coleman Aug 12 '12 at 06:31
  • Alright, I did just successfully set my classloader as the system class loader, and can override getResource to print out that its trying to get the resource, and it did print it out when I ran it, but what URL would I return to represent data, not a specific path? – Alex Coleman Aug 12 '12 at 06:54
  • "their bytes into my bcel loader" so I'm assuming that it must be using some kind of class loader to achieve this – MadProgrammer Aug 12 '12 at 07:02
  • Yes, I am. I think I've got it all setup now (See my edit). Except I've got the protocol set up now too, but now the file it uses uses JNI, and is screwing up cuz i dont have right path variables. Ill get to that later, think this whole issue is cleared up tho – Alex Coleman Aug 12 '12 at 07:26

0 Answers0