MOTODEV // TECHNICAL ARTICLES
Welcome to MOTODEV // Please log in.

Changing JAR Files Manually

Background

Sometimes it becomes necessary to make a small change to a MIDlet post compile time and outside of an IDE. For example, the MIDlet name may need to be changed and the MIDlet source code or MIDlet project files are not available. This article demonstrates how such a change can be implemented without recompiling the MIDlet in an IDE and without the original MIDlet source code.

MIDlet structure and limitations

A MIDlet consists of two files:

  • JAD file – Java Application Descriptor
  • JAR file – Java Archive

The JAD file is a textual file containing attributes which describe the MIDlet.

The JAR file is the binary containing the byte code that will be executed by the Virtual Machine. Within the JAR file, in addition to the binary files, there is a file called MANIFEST.MF which is a file describing the MIDlet, and is very similar to the JAD file.

It is critical that the attributes contained in the JAD exactly match those in the MANIFEST. They do not necessarily have to contain the same attributes, but if they do, then the values of the attributes MUST match. If they do not match, then the MIDlet installation will fail.

If the MANIFEST file is modified using a tool such as WinZip, the MIDlet will not install. A manual modification method is discussed in this article.

Ways to modify a JAR file manually

In this section, a MIDlet called HeadlessTest will be modified. The JAR file is called HeadlessTest.jar. The MIDlet name will be changed to HeadlessTest_New.

Using Winzip and the JAR command

  1. Open the JAR file with WinZip.

    Here, we assume you installed the standalone SDK.

  2. Create a folder under the emulator directory, for example SDK/EmulatorA.6/test.
  3. Extract all the files into that folder.
  4. Copy the manifest file from “META-INF” folder, and then delete this folder.

    Below is the content of the manifest file.

      
      

    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.6.5
    Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
    MIDlet-1: HeadlessTest, , HeadlessTest
    MIDlet-Vendor: Vendor
    MIDlet-Version: 1.0
    MIDlet-Name: HeadlessTest
    MicroEdition-Configuration: CLDC-1.1
    MicroEdition-Profile: MIDP-2.0

  5. Change the “MIDlet-1” attribute as shown next:

    MIDlet-1: HeadlessTest_New, , HeadlessTest

    The code shown next can be used to package all the classes and the manifest file into a JAR file.

    jar cfm HeadlessTest.jar manifest.mf *.class

    If your MIDlet also contains other resources, such as a mid file, you may use the command line to get them into a jar file, too.

    jar uf HeadlessTest.jar *.mid

Using Winzip

  1. 1 Open the JAR file (HeadlessTest.jar) with WinZip.

    Here, assume you installed the standalone SDK.

  2. Create a folder under the emulator directory, for example SDK/EmulatorA.6/test. Extract all the files into that folder.

    Notice you have a folder called META-INF and within it, a file called MANIFEST.MF. Shown next is the content of the manifest file.

    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.6.5
    Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
    MIDlet-1: HeadlessTest, , HeadlessTest
    MIDlet-Vendor: Vendor
    MIDlet-Version: 1.0
    MIDlet-Name: HeadlessTest
    MicroEdition-Configuration: CLDC-1.1
    MicroEdition-Profile: MIDP-2.0

  3. Change the “MIDlet-1” attribute as shown next:

    MIDlet-1: HeadlessTest_New, , HeadlessTest

  4. Rename the old JAR file from HeadlessTest.jar to HeadlessTest.old to retain a copy for backup purposes.
  5. Use winzip to zip all the MIDlet files back into a jar file called HeadlessTest.zip (retaining the original MIDlet internal folder structure).
  6. Rename HeadlessTest.zip to HeadlessTest.jar.
  7. In Windows explorer, right-click on the new HeadlessTest.jar file and select properties.
    You will see a “HeadlessTest.jar Properties” pop-up dialog. Ensure the General Tab is selected and make a note of the value in the parentheses in the size field. The important number to note here is 1484.

    Size: 1.44KB (1,484 bytes)

  8. Open the JAD file (HeadlessTest.jad) and amend the “MIDlet-Jar-Size” attribute to equal the size noted in the previous step.
        

    MIDlet-Jar-Size: 1484

  9. With the JAD file still open, amend the MIDlet-1 attribute to match the one changed in the MANIFEST.MF (manifest file code shown after step 2).
        

    MIDlet-1: HeadlessTest_New, , HeadlessTest

  10. Save the JAD file.

A Quick Batch File

Finally, for your convenience, you can copy the command lines shown next into a batch file and put it into SDK/EmulatorA.6/test. The next time you will only need to change the jar file name in the first line and jad file name in the second line and run the batch file. Everything will be done automatically for you. The MIDlet-Jar-Size attribute in JAD file will also be modified automatically according to the jar file size.

set COMPILETARG=HeadlessTest.jar set JAD = HeadlessTest.jad

@if NOT EXIST *.class goto COMEND jar cfm %COMPILETARG% manifest.mf *.class

@if NOT EXIST res goto CHECKPNG jar uf %COMPILETARG% Res\*.*

:CHECKPNG @if NOT EXIST *.png goto CHECKJPG jar uf %COMPILETARG% *.png

:CHECKJPG @if NOT EXIST *.jpg goto CHECKWAV jar uf %COMPILETARG% *.jpg

:CHECKWAV @if NOT EXIST *.wav goto CHECKMID jar uf %COMPILETARG% *.wav

:CHECKMID @if NOT EXIST *.mid goto CHECKAMR jar uf %COMPILETARG% *.mid

:CHECKAMR @if NOT EXIST *.amr goto JARCOMPLETE jar uf %COMPILETARG% *.amr

:JARCOMPLETE @echo Preverifying ... @echo off

@rem *************************************************************** @rem ** Use the preverifier that is supplied with the emulator @rem ** @rem *************************************************************** @echo off

..\bin\preverify -classpath ..\lib;..\bin;..\lib\stubclasses.zip -d . %COMPILETARG%

@rem *************************************************************** @rem ** Extract the preverified classes from the .JAR file. These @rem ** will be runnable if there are no resources involved. @rem *************************************************************** @echo off

@echo Restoring preverified classes... jar xf %COMPILETARG% *.class

@rem *************************************************************** @rem ** The MIDlet-Jar-Size attribute in JAD file will also be @rem ** modified automatically according to the jar file size. @rem ***************************************************************

for /F %%I IN ("%COMPILETARG%") do set size=%%~zI

@echo size = %size%

@if EXIST test.txt del test.txt > NUL

for /F "delims=: tokens=1*" %%I IN (%JAD%) do (
if %%I EQU MIDlet-Jar-Size (
echo %%I:%size% >> "test.txt"
) ELSE (
echo %%I:%%J >> "test.txt"
)
)

copy/y test.txt %JAD% > NUL
@if EXIST test.txt del test.txt > NUL

:COMEND

pause

 

Note

NOTE: This method does not work if the MIDlet has been digitally signed. If you modify the jar file using this method, you will need to re-sign the MIDlet.

Conclusion

It is possible to modify MIDlet properties after compilation, without having the source code, by using simple tools. This article discusses several ways in which this can be achieved.

Back to Top
Was This Document Helpful?
Yes  No 

Additional Comments (Optional)