Thread: Get class bytes? (Java)

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Get class bytes? (Java)

    Well, as much as I hate to crosspost, this question only seemed to provoke confusion from the folks at the Java Developers Network Forum. Basically, after loading some class (which could of course reside on a remote machine, inside an archive, etc) through a ClassLoader object, I then need to retrieve the raw .class (format) data. The Class class appears to only provide indirect means to access the data, but what I really need is the entire .class file.

    Any idea what API would accomplish this?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Hmm... I don't think there is a straight-forward way of doing this. What you may be able to do is replace the System ClassLoader with your own class. The System ClassLoader is the class which loads all the user defined classes from the CLASSPATH. You can read more about it here. I don't know if this would give you access to the raw .class file bytes though.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    You could try something like this:

    public static byte[] getClassBytes(Class clazz) throws IOException
    {
    String name = clazz.getName().replace('.', '/') + ".class";
    InputStream iStream = clazz.getClassLoader().getResourceAsStream(name);
    try
    {
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();

    Streams.copy(iStream, oStream);
    return oStream.toByteArray();
    }
    finally
    {
    iStream.close();
    }
    }

  4. #4
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72

    Exclamation

    Hello sdaubin,
    Please, put the code snippet in the code tags.

    Ok,
    Nathan Paulino Campos

    Quote Originally Posted by sdaubin View Post
    You could try something like this:

    public static byte[] getClassBytes(Class clazz) throws IOException
    {
    String name = clazz.getName().replace('.', '/') + ".class";
    InputStream iStream = clazz.getClassLoader().getResourceAsStream(name);
    try
    {
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();

    Streams.copy(iStream, oStream);
    return oStream.toByteArray();
    }
    finally
    {
    iStream.close();
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why Virtual base class increase the size of class by 4 bytes?
    By vaibhavs17 in forum C++ Programming
    Replies: 14
    Last Post: 07-09-2009, 06:55 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM