Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting error Incompatible magic value 1784772193 in class file

 
sanyam Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,


I have created a custom loader. while reading class files form disk compiler throws an error "Exception in thread "AWT-EventQueue-0" java.lang.ClassFormatError: Incompatible magic value 1784772193 in class file.....

at java.lang.ClassLoader.defineClass1(Native Method)" It also includes the line of code containing the statement "classTemp= defineClass( classFilename, raw, 0, raw.length);" please suggest some solution

Thanks in advance


 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

This message usually indicates that you used a higher java version to compile the code, than the java version you are using to run it.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so. The error message for that is different: "unsupported class version". This one does mean that there's something wrong with the class files. Can you show us how you create a byte[] from the class file?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops!
Rob is right. As usual I am not thinking straight. Time for me to reboot.
 
sanyam Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I don't think so. The error message for that is different: "unsupported class version". This one does mean that there's something wrong with the class files. Can you show us how you create a byte[] from the class file?



This is the method I have created to convert a file to a byte array



Please Help!!
 
sanyam Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Welcome to the Ranch.

This message usually indicates that you used a higher java version to compile the code, than the java version you are using to run it.




Yes Maneesh I found this on Internet and I recompiled my classes to be loaded and and again re-ran the program.. but still I encountered the same problem

Here is the complete error I am getting again and again..

Exception in thread "AWT-EventQueue-0" java.lang.ClassFormatError: Incompatible magic value 1784772193 in class file DeckOfCards/class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at classLoader.ClassLoaderOfExtClass.loadClassCustom(ClassLoaderOfExtClass.java:161)
at guiTool.SeQuenceDiagramGeneration.jMenuItem1ActionPerformed(SeQuenceDiagramGeneration.java:136)
at guiTool.SeQuenceDiagramGeneration.access$0(SeQuenceDiagramGeneration.java:100)
at guiTool.SeQuenceDiagramGeneration$1.actionPerformed(SeQuenceDiagramGeneration.java:70)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Please Help!!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using a String as an intermediary for binary data. That's just plain wrong. And in any case, the tostring() method of FileInputStream is inherited from Object -- what made you think that would return the content of the File?

You need to read the FIle into a byte array and use that to define the class. Also, is the 'classFilename' going to be the same as he expected binary name of the class? Think about it.
 
sanyam Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:You're using a String as an intermediary for binary data. That's just plain wrong. And in any case, the tostring() method of FileInputStream is inherited from Object -- what made you think that would return the content of the File?

You need to read the FIle into a byte array and use that to define the class. Also, is the 'classFilename' going to be the same as he expected binary name of the class? Think about it.



Thank you so much Darryl !! Yes you were right now it is working perfectly fine

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you have it working, you may like to take a look at File Class Loader.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl, I don't like your way of reading the file. There are two things that can go wrong:
1) fis.available() doesn't necessarily return the file size. Using (int)file.length() is safer. Only if the file is over 2TB large will that fail, but the entire file couldn't be put into a byte[] anyway.
2) fis.read(bytes) can often read less than the requested number of bytes. I'd instead use a DataInputStream around the FileInputStream and use its readFully method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic