blob: d988e2f4be6dbc2b1399406d6087b460ccf7a18a [file] [log] [blame]
<title>Basic Dalvik VM Invocation</title>
<h1>Basic Dalvik VM Invocation</h1>
On an Android device, the Dalvik virtual machine usually executes embedded
in the Android application framework. It's also possible to run it directly,
just as you would a virtual machine on your desktop system.
After compiling your Java language sources, convert and combine the .class
files into a DEX file, and push that to the device. Here's a simple example:
% <font color="green">echo 'class Foo {'\</font><br>
&gt; <font color="green">'public static void main(String[] args) {'\</font><br>
&gt; <font color="green">'System.out.println("Hello, world"); }}' &gt;</font><br>
% <font color="green">javac</font><br>
% <font color="green">dx --dex --output=foo.jar Foo.class</font><br>
% <font color="green">adb push foo.jar /sdcard</font><br>
% <font color="green">adb shell dalvikvm -cp /sdcard/foo.jar Foo</font><br>
Hello, world
The <code>-cp</code> option sets the classpath. The initial directory
for <code>adb shell</code> may not be what you expect it to be, so it's
usually best to specify absolute pathnames.
The <code>dx</code> command accepts lists of individual class files,
directories, or Jar archives. When the <code>--output</code> filename
ends with <code>.jar</code>, <code>.zip</code>, or <code>.apk</code>,
a file called <code>classes.dex</code> is created and stored inside the
Run <code>adb shell dalvikvm -help</code> to see a list of command-line
<h2>Using a debugger</h2>
You can debug stand-alone applications with any JDWP-compliant debugger.
There are two basic approaches.
The first way is to connect directly through TCP. Add, to the "dalvikvm"
invocation line above, an argument like:
This tells the VM to wait for a debugger to connect to it on TCP port 8000.
You need to tell adb to forward local port 8000 to device port 8000:
<code>% <font color="green">adb forward tcp:8000 tcp:8000</font></code>
and then connect to it with your favorite debugger (using <code>jdb</code>
as an example here):
<code>% <font color="green">jdb -attach localhost:8000</font></code>
When the debugger attaches, the VM will be in a suspended state. You can
set breakpoints and then tell it to continue.
You can also connect through DDMS, like you would for an Android application.
Add, to the "dalvikvm" command line:
Note the <code>transport</code> has changed, and you no longer need to
specify a TCP port number. When your application starts, it will appear
in DDMS, with "?" as the application name. Select it in DDMS, and connect
to it as usual, e.g.:
<code>% <font color="green">jdb -attach localhost:8700</font></code>
Because command-line applications don't include the client-side
DDM setup, features like thread monitoring and allocation tracking will not
be available in DDMS. It's strictly a debugger pass-through in this mode.
See <a href="debugger.html">Dalvik Debugger Support</a> for more information
about using debuggers with Dalvik.
<address>Copyright &copy; 2009 The Android Open Source Project</address>