您的位置:首页 > 编程语言 > Java开发

dx编译Hello.java的总结

2016-04-07 19:53 507 查看
dx编译好的相关文件(Hello.java,Hello.class,Hello.class_disassemble,Hello.dex,Hello.jar,Hello.dex_disassemble)链接:
http://download.csdn.net/detail/zhangmiaoping23/9484272
一、首先介绍几条命令:

将Hello.java文件编译成Hello.class文件

F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>javac -source 1.6 -target 1.6 Hello.java

public class Hello {
public int foo(int a, int b) {
return (a+b)*(a-b);
}
public static void main(String[] argc) {
Hello h = new Hello();
System.out.println(h.foo(5, 3));
}
}


将Hello.class文件反编译java汇编代码

F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>javap -c -classpath . Hello

Compiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: aload_0
1: invokespecial #1                  // Method java/lang/Object."<init>":()V
4: return

public int foo(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: iload_1
4: iload_2
5: isub
6: imul
7: ireturn

public static void main(java.lang.String[]);
Code:
0: new           #2                  // class Hello
3: dup
4: invokespecial #3                  // Method "<init>":()V
7: astore_1
8: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
11: aload_1
12: iconst_5
13: iconst_3
14: invokevirtual #5                  // Method foo:(II)I
17: invokevirtual #6                  // Method java/io/PrintStream.println:(I)V
20: return
}


将Hello.class文件编译成Hello.dex文件

F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>dx --dex --output Hello.dex Hello.class

将.dex文件反编译成dalvik汇编代码

F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>dexdump.exe -d Hello.dex

Processing 'hello.dex'...
Opened 'hello.dex', DEX version '035'
Class #0            -
Class descriptor  : 'LHello;'
Access flags      : 0x0001 (PUBLIC)
Superclass        : 'Ljava/lang/Object;'
Interfaces        -
Static fields     -
Instance fields   -
Direct methods    -
#0              : (in LHello;)
name          : '<init>'
type          : '()V'
access        : 0x10001 (PUBLIC CONSTRUCTOR)
code          -
registers     : 1
ins           : 1
outs          : 1
insns size    : 4 16-bit code units
00014c:                                        |[00014c] Hello.<init>:()V
00015c: 7010 0400 0000                         |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0004
000162: 0e00                                   |0003: return-void
catches       : (none)
positions     :
0x0000 line=1
locals        :
0x0000 - 0x0004 reg=0 this LHello;

#1              : (in LHello;)
name          : 'main'
type          : '([Ljava/lang/String;)V'
access        : 0x0009 (PUBLIC STATIC)
code          -
registers     : 5
ins           : 1
outs          : 3
insns size    : 17 16-bit code units
000164:                                        |[000164] Hello.main:([Ljava/lang/String;)V
000174: 2200 0100                              |0000: new-instance v0, LHello; // type@0001
000178: 7010 0000 0000                         |0002: invoke-direct {v0}, LHello;.<init>:()V // method@0000
00017e: 6201 0000                              |0005: sget-object v1, Ljava/lang/System;.out:Ljava/io/PrintStream; // field@0000
000182: 1252                                   |0007: const/4 v2, #int 5 // #5
000184: 1233                                   |0008: const/4 v3, #int 3 // #3
000186: 6e30 0100 2003                         |0009: invoke-virtual {v0, v2, v3}, LHello;.foo:(II)I // method@0001
00018c: 0a00                                   |000c: move-result v0
00018e: 6e20 0300 0100                         |000d: invoke-virtual {v1, v0}, Ljava/io/PrintStream;.println:(I)V // method@0003
000194: 0e00                                   |0010: return-void
catches       : (none)
positions     :
0x0000 line=6
0x0005 line=7
0x0010 line=8
locals        :

Virtual methods   -
#0              : (in LHello;)
name          : 'foo'
type          : '(II)I'
access        : 0x0001 (PUBLIC)
code          -
registers     : 5
ins           : 3
outs          : 0
insns size    : 6 16-bit code units
000198:                                        |[000198] Hello.foo:(II)I
0001a8: 9000 0304                              |0000: add-int v0, v3, v4
0001ac: 9101 0304                              |0002: sub-int v1, v3, v4
0001b0: b210                                   |0004: mul-int/2addr v0, v1
0001b2: 0f00                                   |0005: return v0
catches       : (none)
positions     :
0x0000 line=3
locals        :
0x0000 - 0x0006 reg=2 this LHello;

source_file_idx   : 1 (Hello.java)


dx使用出现的错误总结

  使用dx的各种悲剧错误,一直一直搞啊搞,搞了三天之后才把这么个小错误搞定

  做程序员不易浮躁啊,还有就是要真的是要锲而不舍啊!!!!!!!

  错误一:

  UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.RuntimeException: abc.class: file not found at com.android.dx.util.FileUtils.readFile(FileUtils.java:55) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.j
ava:134) at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java :109) at comand.dexer.Main.processOne(Main.java:422) at comand.dexer.Main.processAllFiles(Main.java:333) at comand.dexer.Main.run(Main.java:209) at comand.dexer.Main.main(Main.java:174)
at comand.Main.main(Main.java:91) 1 error; aborting

  


  当你出现这个运行时错误的话,那就是你需要编译的文件需要和你安卓的platform-tool这个文件夹中的内容放在一起

注:现在版本是在sdk\build-tools\XX.X.X目录下,dx即是dx.bat.

  你要做的就是把你的文件放在和dexdump.exe文件和dx.bat放在同一个目录下

注:除了文件放在目录,工作目录必须也得改。即

E:\Android\sdk\build-tools\23.0.1>dx --dex --output Hello.jar Hello.class

  


  这个样子就可以编译通过成功生成dex文件了

  错误二:

  trouble processing: bad class file magic (cafebabe) or version (0033.0000) ...while parsing Hello.class ...while processing Hello.class 1 warning
no classfiles specified

  出现这个错误的原因是因为jdk的版本不对,我之前用的是JDK1.7但是不知道为什么他编译之后生成的JDK的版本会低于jdk1.6真是让我百思不得其解?

  也不知道这个代码是怎么写的

  之后我上网查了一下,我把JDK1.7换成了JDK1.6,可是还是不行,出现了相同的提示,结果展示如下

  


  这个时候解决的办法有一个:

  强制转换成1.6版本的class

  使用的到的命令就是javac -source 1.6 -target 1.6 ****.java(****代表了你的java文件名称)

  具体步骤演示如下:

  1.编写一个java文件

  2.使用javac命令:javac -source 1.6 -target 1.6 ******.java 注意你编写的必须是在你的android的platform-tools文件目录下

  3.使用dx命令 dx --dex --output =*****.dex *****.class

  dexdump.exe -d *****.dex

  这个时候我们来看一下结果

  D:\Android\android-sdk\platform-tools>dx --dex --output=abc.dex abc.class D:\Android\android-sdk\platform-tools>dexdump.exe -d abc.dex Processing 'abc.dex'... Opened 'abc.dex', DEX version '035' Class #0 - Class descriptor : 'Labc;' Access flags : 0x0001
(PUBLIC) Superclass : 'Ljava/lang/Object;' Interfaces - Static fields - Instance fields - Direct methods - #0 : (in Labc;) name : '<init$amp;>apos;$ type : '()V' access : 0x10001 (PUBLIC CONSTRUCTOR) code - registers : 1 ins : 1 outs : 1 insns size : 4 16-bit
code units 000160: |[000160] abc.<init>:()V 000170: 7010 0500 0000 |0000: invoke-direct {v0}, Ljava/ lang/Object;.<init>:()V // method@0005 000176: 0e00 |0003: return-void catches : (none) positions : 0x0000 line=2 locals : 0x0000 - 0x0004 reg=0 this Labc;
#1 : (in Labc;) name : 'main' type : '([Ljava/lang/String;)V' access : 0x0009 (PUBLIC STATIC) code - registers : 3 ins : 1 outs : 2 insns size : 30 16-bit code units 000178: |[000178] abc.main:([Ljava/lang/S tring;)V 000188: 1250 |0000: const/4 v0, #int 5
// #5 00018a: 1271 |0001: const/4 v1, #int 7 // #7 00018c: 2311 0600 |0002: new-array v1, v1, [I // ty pe@0006 000190: 2601 0800 0000 |0004: fill-array-data v1, 000000 0c // +00000008 000196: 7120 0200 1000 |0007: invoke-static {v0, v1}, La bc;.test:(I[I)V
// method@0002 00019c: 0e00 |000a: return-void 00019e: 0000 |000b: nop // spacer 0001a0: 0003 0400 0700 0000 0100 0000 0100 ... |000c: array-data (18 units) catches : (none) positions : 0x0000 line=18 0x000a line=19 0x000b line=18 locals : #2 : (in Labc;)
name : 'test' type : '(I[I)V' access : 0x0089 (PUBLIC STATIC VARARGS) code - registers : 6 ins : 2 outs : 2 insns size : 20 16-bit code units 0001c4: |[0001c4] abc.test:(I[I)V 0001d4: 2151 |0000: array-length v1, v5 0001d6: 1200 |0001: const/4 v0, #int 0 //
#0 0001d8: 3510 0c00 |0002: if-ge v0, v1, 000e // +000 c 0001dc: 4402 0500 |0004: aget v2, v5, v0 0001e0: 6203 0000 |0006: sget-object v3, Ljava/lang /System;.out:Ljava/io/PrintStream; // field@0000 0001e4: 6e20 0300 2300 |0008: invoke-virtual {v3, v2}, L
java/io/PrintStream;.print:(I)V // method@0003 0001ea: d800 0001 |000b: add-int/lit8 v0, v0, #int 1 // #01 0001ee: 28f5 |000d: goto 0002 // -000b 0001f0: 6200 0000 |000e: sget-object v0, Ljava/lang /System;.out:Ljava/io/PrintStream; // field@0000 0001f4: 6e20
0400 4000 |0010: invoke-virtual {v0, v4}, L java/io/PrintStream;.println:(I)V // method@0004 0001fa: 0e00 |0013: return-void catches : (none) positions : 0x0000 line=10 0x0006 line=12 0x000b line=10 0x000e line=14 0x0013 line=15 locals : Virtual methods -
source_file_idx : 12 (abc.java)

  如果你有任何的问题,可以和我联系:

  之前走了好多弯路希望能帮上大家!^^

参考:
http://www.educity.cn/wenda/152600.html (dx使用出现的异常总结)
http://www.tuicool.com/articles/7ZrMjm (使用dx命令在cmd环境下执行的正确方法,我用的版本android4.4.2,jdk1.8
- colin1983)



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: