• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Getting error Incompatible magic value 1784772193 in class file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i have the same problem, where i want to compile java source code and store bytecode directly in an object.
bytecode store in byte[] array but when i use defineClass method from ClassLoader class then "java.lang.ClassFormatError: Extra bytes at the end of class file Main" this error accur.




what's wrong with it?
 
Sheriff
Posts: 28329
96
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any code which actually writes bytes to an output stream, but my guess is that it's writing the bytes one buffer at a time and not paying attention to the fact that the last buffer may not be totally full. So I'd suggest drilling down in the code until you find where that's being done.
 
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Paul said, I would eyeball the values in the byte array and check:
  - that the array begins with the magic number 0xCAFEBABE
  - if there is anything odd near the end of the array like a series of zero values or a string of bytes which also appeared earlier-on on the array which might indicate that the whole buffer was written to the file and not just the portion of it needed for the last write (Paul's guess)
  - for ASCII text (other than String constants which may have been in the source) which may have been written to the output of an error occurred during complication
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic