Search code examples
javadecompilingurlclassloader

Save class to file from URLClassLoader? and decompile it?


It's possible to save class from URLClassLoader to normal file.class? and then decompile it?

I trying save it just as object using

        Class<?> clazz = classLoader.loadClass("foo.Bar");
        FileOutputStream sf = new FileOutputStream(f);
        ObjectOutputStream s = new ObjectOutputStream(sf);
        s.writeObject(clazz);
        s.close();

Bu that don't work.

So... how to decompile it? I need get something like result of jd-gui, but using class from URLClassLoader.


Solution

  • You need to map the class name (e.g. "foo.Bar") to a resource path name (e.g. "/foo/Bar.class") and then use classLoader.getResourceAsStream to open a stream to read the bytecode file.

    In theory, this can then be fed to a decompiler ... assuming that you have a decompiler that can read from an InputStream.


    What you are doing at the moment fails because a Class object cannot be serialized.