Show me the code! – By Davanum Srinivas

December 4, 2007

Command line Java on DalvikVM

Filed under: Uncategorized — Davanum Srinivas @ 11:21 am

Found this very useful when i was trying out the JNI under android (Short Story – Could not get it to work!).

Step #1: Start with a simple java class

package org.apache;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Compile the java class

C:\android\CmdLine>javac -d . -g Helloworld.java

Step #2: Package the generated classes into a temporary jar

C:\android\CmdLine>jar -cvf Temp.jar *
added manifest
adding: Hello.java(in = 0) (out= 0)(stored 0%)
adding: HelloWorld.java(in = 149) (out= 122)(deflated 18%)
adding: org/(in = 0) (out= 0)(stored 0%)
adding: org/apache/(in = 0) (out= 0)(stored 0%)
adding: org/apache/HelloWorld.class(in = 556) (out= 344)(deflated 38%)

Step #3: Use the “dx” tool to generate a classes.dex from our temporary jar.

C:\android\CmdLine>dx --dex --output=c:/android/CmdLine/classes.dex c:/android/CmdLine/Temp.jar

Step #4: Use the “aapt” tool to create a new jar suitable for use with dalvikvm

C:\android\CmdLine>aapt add CmdLine.jar classes.dex
 'classes.dex'...

and push it into a known location on the emulator

C:\android\CmdLine>adb push CmdLine.jar /data
30 KB/s (0 bytes in 481.000s)

Step #5: kick the tires of dalvikvm

C:\android\CmdLine>adb shell
# /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -version
/system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -version
DalvikVM version 0.2.0
Copyright (C) 2007 Google, Inc.
Blah blah blah LICENSE blah blah.
Dalvik VM init failed (check log file)

Step #6: Run our code

# /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /data/CmdLine.jar org.apache.HelloWorld
/system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /data/CmdLine.jar org.apache.HelloWorld
Hello World!
About these ads

4 Comments »

  1. Good example and clear instructions, but it doesn’t run if you try to instantiate the class in the main function to do advanced stuff (like have a finalize clause). An error occurs when I attempted to do this:

    Dalvik VM unable to find static main(String[]) …

    Using Android 1.6.

    Comment by Ryan Page — November 20, 2009 @ 12:28 pm

  2. This doesnt make J2ME jars to be run on android is it?
    only simple java jars run on this dalvik java

    Comment by sparco — February 23, 2010 @ 2:19 am

  3. What would be the additional steps needed if the HelloWorld program needs to refer to external classes/packages?

    Comment by Stephen — May 24, 2011 @ 10:48 am

  4. I’m having severe problems with my mobile and being redirected to other web sights..

    Comment by David Burgess — March 27, 2012 @ 2:08 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 100 other followers

%d bloggers like this: