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

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

C:\android\CmdLine>jar -cvf Temp.jar *
added manifest
adding: = 0) (out= 0)(stored 0%)
adding: = 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

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!


  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

  5. Two additional points that were needed for me:
    1) Run dalvikvm as root, otherwise you don’t have permission to write to /data/dalvikvm-cache
    $ su –
    2) add the required LD_LIBRARY_PATH for dalvikvm:
    # export LD_LIBRARY_PATH=/system/lib
    3) run the vm
    # /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /data/CmdLine.jar org.apache.HelloWorld

    To discover this:
    $ logcat *:S dalvikvm:D

    E/dalvikvm( 3400): Dex cache directory isn't writable: /data/dalvik-cache
    I/dalvikvm( 3400): Unable to open or create cache for Hello2.jar (/data/dalvik-cache/storage@sdcard0@tmp@Hello2.jar@classes.dex)

    D/dalvikvm( 4927): Trying to load lib 0x0
    D/dalvikvm( 4927): Added shared lib 0x0
    D/dalvikvm( 4927): Trying to load lib 0x0
    D/dalvikvm( 4927): Added shared lib 0x0
    W/dalvikvm( 4927): Exception Ljava/lang/NullPointerException; thrown while initializing Ljava/lang/System;

    Comment by vanrenter — December 8, 2013 @ 8:39 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: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

Blog at

%d bloggers like this: