1<html>
2<head>
3    <title>Basic Dalvik VM Invocation</title>
4</head>
5
6<body>
7<h1>Basic Dalvik VM Invocation</h1>
8
9<p>
10On an Android device, the Dalvik virtual machine usually executes embedded
11in the Android application framework.  It's also possible to run it directly,
12just as you would a virtual machine on your desktop system.
13</p><p>
14After compiling your Java language sources, convert and combine the .class
15files into a DEX file, and push that to the device.  Here's a simple example:
16
17</p><p><code>
18% <font color="green">echo 'class Foo {'\</font><br>
19&gt; <font color="green">'public static void main(String[] args) {'\</font><br>
20&gt; <font color="green">'System.out.println("Hello, world"); }}' &gt; Foo.java</font><br>
21% <font color="green">javac Foo.java</font><br>
22% <font color="green">dx --dex --output=foo.jar Foo.class</font><br>
23% <font color="green">adb push foo.jar /sdcard</font><br>
24% <font color="green">adb shell dalvikvm -cp /sdcard/foo.jar Foo</font><br>
25Hello, world
26</code>
27</p><p>
28The <code>-cp</code> option sets the classpath.  The initial directory
29for <code>adb shell</code> may not be what you expect it to be, so it's
30usually best to specify absolute pathnames.
31
32</p><p>
33The <code>dx</code> command accepts lists of individual class files,
34directories, or Jar archives.  When the <code>--output</code> filename
35ends with <code>.jar</code>, <code>.zip</code>, or <code>.apk</code>,
36a file called <code>classes.dex</code> is created and stored inside the
37archive.
38</p><p>
39Run <code>adb shell dalvikvm -help</code> to see a list of command-line
40options.
41</p><p>
42
43
44
45<h2>Using a debugger</h2>
46
47<p>
48You can debug stand-alone applications with any JDWP-compliant debugger.
49There are two basic approaches.
50</p><p>
51The first way is to connect directly through TCP.  Add, to the "dalvikvm"
52invocation line above, an argument like:
53</p><p>
54<code>&nbsp;&nbsp;-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y</code>
55</p><p>
56This tells the VM to wait for a debugger to connect to it on TCP port 8000.
57You need to tell adb to forward local port 8000 to device port 8000:
58</p><p>
59<code>% <font color="green">adb forward tcp:8000 tcp:8000</font></code>
60</p><p>
61and then connect to it with your favorite debugger (using <code>jdb</code>
62as an example here):
63</p><p>
64<code>% <font color="green">jdb -attach localhost:8000</font></code>
65</p><p>
66When the debugger attaches, the VM will be in a suspended state.  You can
67set breakpoints and then tell it to continue.
68
69
70</p><p>
71You can also connect through DDMS, like you would for an Android application.
72Add, to the "dalvikvm" command line:
73</p><p>
74<code>&nbsp;&nbsp;-agentlib:jdwp=transport=dt_android_adb,suspend=y,server=y</code>
75</p><p>
76Note the <code>transport</code> has changed, and you no longer need to
77specify a TCP port number.  When your application starts, it will appear
78in DDMS, with "?" as the application name.  Select it in DDMS, and connect
79to it as usual, e.g.:
80</p><p>
81<code>% <font color="green">jdb -attach localhost:8700</font></code>
82</p><p>
83Because command-line applications don't include the client-side
84DDM setup, features like thread monitoring and allocation tracking will not
85be available in DDMS.  It's strictly a debugger pass-through in this mode.
86</p><p>
87See <a href="debugger.html">Dalvik Debugger Support</a> for more information
88about using debuggers with Dalvik.
89
90
91</p></p>
92<address>Copyright &copy; 2009 The Android Open Source Project</address>
93
94</body>
95</html>
96