Skip to content

Commit 7d5b5ce

Browse files
author
sshfuture
committed
jvm虚拟机 第一次作业
1 parent f06842f commit 7d5b5ce

File tree

5 files changed

+73
-44
lines changed

5 files changed

+73
-44
lines changed
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package com;
22

3+
34
/**
45
* Created by hushuai on 2017/3/9.
56
*/
67
public class TestMain {
78
public static void main(String[] args) {
9+
810
System.out.println(111);
911

1012
}
11-
//
12-
// class downloadThread implements Runnable{
13-
// @Override
14-
// public void run() {
15-
//
16-
// }
17-
// }
1813
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coderising.jvm.loader;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
8+
9+
public class ClassFileLoader {
10+
11+
private List<String> clzPaths = new ArrayList<String>();
12+
13+
public byte[] readBinaryCode(String className) {
14+
String filePath = getClassPath()+className.replace(".","\\")+".class";
15+
byte[] itemBuf = null;
16+
File file = new File(filePath);
17+
InputStream in = null;
18+
try {
19+
in = new FileInputStream(file);
20+
21+
itemBuf = new byte[(int)file.length()];
22+
in.read(itemBuf, 0, (int)file.length());
23+
24+
25+
in.close();
26+
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}finally {
30+
if (in != null) {
31+
try {
32+
in.close();
33+
} catch (IOException e1) {
34+
}
35+
}
36+
}
37+
return itemBuf;
38+
39+
40+
}
41+
42+
43+
public void addClassPath(String path) {
44+
clzPaths.add(path);
45+
}
46+
47+
48+
49+
public String getClassPath(){
50+
String result = null;
51+
StringBuffer sb = new StringBuffer();
52+
for(String classPath : this.clzPaths){
53+
sb.append(classPath+";");
54+
}
55+
if(sb.length() != 0){
56+
result = sb.toString();
57+
result = result.substring(0,result.length()-1);
58+
59+
}
60+
61+
return result;
62+
}
63+
64+
65+
66+
67+
68+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class ClassFileloaderTest {
1515

1616

17-
static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin";
17+
static String path1 = "D:\\mywork\\coding2017\\group18\\744888802\\target\\classes";
1818
static String path2 = "C:\temp";
1919

2020

@@ -46,7 +46,7 @@ public void testClassFileLength() {
4646
ClassFileLoader loader = new ClassFileLoader();
4747
loader.addClassPath(path1);
4848

49-
String className = "com.coderising.jvm.test.EmployeeV1";
49+
String className = "\\com.coderising.jvm.test.EmployeeV1";
5050

5151
byte[] byteCodes = loader.readBinaryCode(className);
5252

@@ -60,7 +60,7 @@ public void testClassFileLength() {
6060
public void testMagicNumber(){
6161
ClassFileLoader loader = new ClassFileLoader();
6262
loader.addClassPath(path1);
63-
String className = "com.coderising.jvm.test.EmployeeV1";
63+
String className = "\\com.coderising.jvm.test.EmployeeV1";
6464
byte[] byteCodes = loader.readBinaryCode(className);
6565
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};
6666

group18/744888802/src/main/java/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)