Home arrow static arrow Java Programming [Archive] - java.lang.ClassFormatError: Bad magic number
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java.lang.ClassFormatError: Bad magic number
This topic has 9 replies on 1 page.

Posts:73
Registered: 7/13/03
java.lang.ClassFormatError: Bad magic number  
Jul 11, 2004 1:29 PM



 
Hi all,
I want to load a class from a .class-file. My code throws a ClassFormatError. Could anyone help me? Seems to be a problem with the byte-array.

[code]
private static boolean parseFile(File file) throws IOException, FileNotFoundException {
if (! file.getName().endsWith(".class"))
return false;

StringBuffer buffer = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(file));
while ((in.readLine()) != null) {
buffer.append(in.readLine());
}
byte[] bytes = buffer.toString().getBytes();
Class c = MyClassLoader.getInstance().getMyClass(bytes);

// do more stuff with the class...
return true;
}

public class MyClassLoader extends ClassLoader {

private static MyClassLoader instance = new MyClassLoader();

public static MyClassLoader getInstance() {
return instance;
}

public Class getMyClass(byte[] bytes) {
return super.defineClass(null, bytes, 0, bytes.length);
}
}
[code]
 

Posts:1,183
Registered: 1/23/02
Re: java.lang.ClassFormatError: Bad magic number  
Jul 11, 2004 1:41 PM (reply 1 of 9)



 
Do not use Readers with binary data. Readers translate bytes to characters and vice versa, which obviously screws up binary data.

Use BufferedInputStream/FileInputStream combo. You can accumulate bytes in a BytaArrayOutputStream or ByteBuffer.
 

Posts:11,200
Registered: 7/22/99
Re: java.lang.ClassFormatError: Bad magic number  
Jul 11, 2004 1:41 PM (reply 2 of 9)



 
The Reader classes are meant for reading simple text data. They should not be used to read arbitrary binary data. The InputStream and RandomAccessFile classes are meant for that.

The simplest way to read a file to an in-memory byte array IMO is using RandomAccessFile:
int fileLength = (int) file.length(); // assume file size < 2GBbyte[] contents = new byte[fileLength]; // allocate memory for the file's dataRandomAccessFile raf = new RandomAccessFile(file, "r");raf.readFully(contents); // read all data from file to memoryraf.close();// you can use the byte array now
 

Posts:73
Registered: 7/13/03
Re: java.lang.ClassFormatError: Bad magic number  
Jul 11, 2004 1:46 PM (reply 3 of 9)



 
Thank you guys!
 

Posts:73
Registered: 7/13/03
Re: java.lang.ClassFormatError: Bad magic number  
Jul 11, 2004 3:45 PM (reply 4 of 9)



 
Hi again,
When a subclass is loaded the parent class is loaded too. .Loading a second subclass causes trouble because the parent class is already loaded. (oops, what a sentence!)
java.lang.LinkageError: duplicate class definition: ch/sbb/paramtool/Handler


How can I handle or prevent this exception? Sorry I'm new to reflection.
Thanks for your help....
 

Posts:1,183
Registered: 1/23/02
Re: java.lang.ClassFormatError: Bad magic number  
Jul 11, 2004 4:52 PM (reply 5 of 9)



 
How can I handle or prevent this exception?

You must cache Classes loaded by your ClassLoader and on subsequent requests for the same name you must return the same Class object.
 

Posts:11,200
Registered: 7/22/99
Re: java.lang.ClassFormatError: Bad magic number  
Jul 12, 2004 12:17 AM (reply 6 of 9)



 
Is there any reason why you're not using URLClassLoader?
 

Posts:73
Registered: 7/13/03
Re: java.lang.ClassFormatError: Bad magic number  
Jul 12, 2004 2:57 AM (reply 7 of 9)



 
No, there is no reason. Maybe I should have a look at URLClassLoader.
Caching the loaded classes works fine, even it's a bit circuitous.

Thanks for your help! Have a nice day.
 

Posts:1,183
Registered: 1/23/02
Re: java.lang.ClassFormatError: Bad magic number  
Jul 12, 2004 5:27 AM (reply 8 of 9)



 
Caching the loaded classes works fine, even it's a bit circuitous.

Being circutous in this case is a requirement. [url http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44459]"Given the same name, a good class loader should always return the same class object"[/url]
 

Posts:73
Registered: 7/13/03
Re: java.lang.ClassFormatError: Bad magic number  
Jul 12, 2004 5:48 AM (reply 9 of 9)



 
My cognition after reading this doc:
My loader is rather malicious than well-behaved.
But it fulfils my needs.

Thanks ;).
 
This topic has 9 replies on 1 page.