Tuesday, March 2, 2010

Samsung - Java Error: Invalid Format. How to fix it.

There is a strange error in Samsung phones. When you try to launch a midlet the phone displays the message "Java Error: Invalid format".

Why?

This means the virtual machine can't load the classes of the midlet. It could be:
- the classes are not preverified
- the classes are corrupted
- the classes are ok but the VM can't load them, because an exception is launched while they are loading..

We will focus in the last option. Why a class can't be loaded and how to fix it?

- Sometimes we write some lines of code in the midlet constructor. But this lines can have method calls to other classes and so on, and all classes must be loaded BEFORE the midlet is initializad. If the midlet is not initialized it can't manage well things like screen.getWidth and getHeight, etc. Avoid method calls in the midlet constructor.
- Other cause can be to write method calls in static blocks or static variables. It's the same problem than before, lots of things must be loaded BEFORE the midlet is instantiated.

So, now the midlet is instantiated before the classes are loaded. Now the error "Invalid Formatd" shouldn't appear. Your phone must launch now an exception or anything similar.

E.g: Samsung S8000 Jet. This phone has a problem in some firmwares. The method Font.getSize() returns a non standard value, and when you want to obtain a new font using the size returned by Font.getSize() an exception is launched.  If this code is called by the constructor block an exception is thrown before the midlet is instantiated, so the error "Invalid Format" is displayed.

I worked with lots of samsungs and generally the solution is the same. Avoid coding in the constructor to find easily the errors.

I hope it works for you.

Cheers,

Lula

19 comments:

  1. Hi, I have an issue with my samsung device...
    I made a modification to a mini opera jar...

    ( I want to access to https:// that´s the reason of the mod ).

    With the original file there are no issues but when its modified I get the error:

    Java Error: Invalid Format.

    Can i send you the .jar file to fix it plz???
    I have done a lot of things but I always get the error. ( alb.arroyo@yahoo.com )

    I hope you can help me.

    ReplyDelete
  2. Hi, i made a modification of mini opera in order to enable the access to "https://".

    The original file runs perfectly but when its modified the error appears: Java Error: Invalid Format.

    Can i send you the .jar file to fix the issue???
    plzzz

    I have done everything but I always get that error... hope you can help me.

    ( alb.arroyo@yahoo.com )

    sorry if the comment appears twice...

    ReplyDelete
  3. Hey!
    I think you are editing the bytecode, isn't it? How did you edit it? To touch a class file you need a bytecode manipulation framework or tool.

    I have made a tool to change strings in a class file, if it's helpful for you I can publish it, but you must not change commercial software.

    ReplyDelete
  4. hey,
    got samsung GT s3653 , i downloaded this dictionary which says that it works on every mobile supported with java which successfully istalled but cant open , dont know why .it says '' java error.invalid format''. I tried it on nokia E65 and it worked

    ReplyDelete
  5. Hi,

    I'm teaching how to fix it editing the source code. You should contact the authors of the dictionary, they have the source so they can fix it quickly.

    Thanks for your feedback.

    ReplyDelete
  6. Hi,

    I'm one of the authors of GpsMid, a navigation application for J2ME using map data from openstreetmap. I know this error from another person's S8000 and would like to fix this issue for it.

    We do bundle map data into the GpsMid midlet, with the JAR containing several hundred, thousands or ten thousands files and the JAR file becoming large. I could get one GpsMid JAR with about 1900 KB and ca. 900 files in the JAR to install and run fine on S8000.

    But that's not even enough for the city I live in. Therefore I've also done a larger midlet with more map data in the JAR (ca. 4900 KB in also about 900 files) but this one starts up with the invalid format error on S8000 while it works fine on e.g. Nokia and SE.

    This is GpsMid's main class: http://gpsmid.git.sourceforge.net/git/gitweb.cgi?p=gpsmid/GpsMid;a=blob;f=src/de/ueller/midlet/gps/GpsMid.java;h=0063f3a0703c4dc75c8a1b8eb68d570117793405;hb=HEAD

    The constructor GpsMid() is empty, everything's done in startApp(). Do you have any idea why the invalid format error comes up with this on larger JARs with more map data bundled?

    Any help would be greatly appreciated.

    sk750

    ReplyDelete
  7. at my mobile it said java-error,
    what do i have to do now?

    ReplyDelete
  8. Hi sk750,

    your constructor is clean, other things that I suggest are:

    - be sure J2MEPolish is not adding anything else in your parent MIDlet. J2MEPolish is a great tool, but it adds a lot of code to avoid fragmentation.

    - check the amount of files is not a limitation of S8000, delete all the resources inside the app and install it again, the app will not work but it should launch a different error (nullpointer or similar because it couldn't find the resource files)

    - check if the classes used by your main midlet have no static blocks. If the KVM load all the linked classes the static blocks will be executed before the main class is loaded.

    Good luck,

    Lula

    ReplyDelete
  9. Thank you for your comments.

    The number of resource files did not increase between the working (1900 kB) and the non-working (4900 kB) midlet. The size difference of the midlet is just caused by the increased size of the contained resource files (especially bigger map tiles and index files). Everything else, i.e. class files / program code remains the same in both midlets.

    So do you think the loading of the midlet might just have become too slow with the large JAR to load the necessary classes just in time before the midlet is instantiated?

    GpsMid also has the option to read the map data from the file system but reading map files from the file system is terribly slow on S8000 while with internal (resource file) map data it's really blazingly fast. If external files could be read faster by S8000, this would be an acceptable workaround.

    ReplyDelete
  10. I didn't understand a thing. My phone gets that message everytime I want to open stuff I downloaded from Argim.net. It sucks.And sometimes it works just by magic! It's something weird. ._.

    ReplyDelete
  11. Hi Lula
    I have a Samsung S5230 and I´m developing an app that plays MP3 files (besides other things). The app works fine on emulator (Netbeans) but on the phone the app just doesn´t produce any sound. It works on the rest, but no sound. Do you know about some bug on this? Can you help me?
    Thanks
    Gustavo

    ReplyDelete
  12. Hi,

    an almost all phones there are two things that you must do when working with sounds:

    1- play sounds in a separated thread. You can create a sound server that waits in a separated thread and when you need to play a sound you set the sound id and then notify the sound server to play it.

    2- Respect the player lifecycle.
    To play the sound:
    realize, prefetch, start
    to close it:
    stop, deallocate, close

    if you do the above you should not have big issues.

    bes regards,

    Lula

    ReplyDelete
  13. I have also samsung c322 phone. It also gives same problem that java error,
    when i try to download any application. Please tell me how to fix this?

    ReplyDelete
  14. I have also samsung c322 pho
    also gives same problem that
    error,
    when i try to download any ap
    Please tell me how to fix this?

    ReplyDelete
  15. Hi Im Xboy i didnt understand ur post but thats my problem on my cellphone too, its Samsung Champ Duos And when i load a jar file it says "Java Error Content Size Large"

    ReplyDelete
  16. Hi Xboy,

    It seems there is a .class file that is too large to load in memory. Split your classes in smaller files.

    ReplyDelete
  17. at my mobile it said java-error,
    what do i have to do now?

    ReplyDelete
  18. hi. i have samsung gt s5230 . when i put the .JAR and .JAD files in games and more using tkfile explorer. when i open it on phone it says java error. unsupported file. what do i do?? i cant understand this one "Avoid method calls in the midlet constructor.
    - Other cause can be to write method calls in static blocks or static variables" that what you say. i cant get it. plsss help Lula

    ReplyDelete
  19. Thanks for the answer. Removing anything from the constructor in my case seems to have done the trick.

    What is the recommended way of fixing this if one does not have access to the source code?

    Thanks

    Josh.

    ReplyDelete