java编译执行有第三方依赖的类

有时候在进行开发的过程中,需要自己写个测试类来进行某个局部功能的测试,在测试的过程中,需要引入第三方jar包或者公司其他成员的帮助类,比如说:我需要测试一个
net.sf.json.JSONObject解析数据的时候中文乱码问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import java.io.*;
import java.net.URLEncoder;

/**
* Created by 神器 on 2017/3/29.
*/
public class ZyhqErrorMsgDemo {

public static void main(String[] args) {
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader in = null;
try{
String filePath = "/home/1.txt";
File file = new File(filePath);
String code = codeString(filePath);
System.out.println("文件类型:" + code);
inputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
in = new BufferedReader(inputStreamReader);
String firstLine = in.readLine();//过滤掉首行,以便循环体从第二行(申赎明细处理结果)
String line = "";
while ((line = in.readLine()) != null) {
System.out.println("line:=====>"+ line);
System.out.println();
System.out.println();
JSONObject jsonObject = JSONObject.fromObject(line);
String status = jsonObject.getString("status");
String objectStr = jsonObject.getString("content");
String errorMessage = null;
try{
errorMessage = jsonObject.get("errorMessage").toString();
}catch (JSONException ex) {
System.out.println("there is no errorMessage from Zyhq holding respon file");
}
System.out.println("errorMessage:------>" + errorMessage);
}
in.close();
inputStreamReader.close();
inputStream.close();
}catch (Exception ex){
System.out.println("下载文件并解析文件内容时出错"+ex.getStackTrace());
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {

}
}
if (inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {

}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {

}
}
}
}

/**
* 判断文件的编码格式
* @param fileName :file
* @return 文件编码格式
* @throws Exception
*/
private static String codeString(String fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;

switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}

return code;
}
}

写好之后上传到服务器上,需要自己在java文件目录上传好依赖的第三方jar包,然后编译:

1
javac -cp commons-beanutils-1.8.3.jar:commons-collections-3.2.1.jar:commons-lang-2.6.jar:commons-logging-1.2.jar:json-lib-2.4-jdk15.jar: ZyhqErrorMsgDemo.java

切记:最后一个jar包后的:后面要加上一个空格再引入自己的Java文件

执行的命令:

1
javac -cp commons-beanutils-1.8.3.jar:commons-collections-3.2.1.jar:commons-lang-2.6.jar:commons-logging-1.2.jar:json-lib-2.4-jdk15.jar: ZyhqErrorMsgDemo