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