-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.java.runtime.xml
More file actions
271 lines (213 loc) · 7.34 KB
/
section.java.runtime.xml
File metadata and controls
271 lines (213 loc) · 7.34 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="Runtime">
<title>exec 运行shell</title>
<screen>
<![CDATA[
exec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.
那个dir就是调用的程序的工作目录,这句其实还是很有用的。
Windows下调用程序
Process proc =Runtime.getRuntime().exec("file.exe");
Linux下调用程序就要改成下面的格式
Process proc =Runtime.getRuntime().exec("./file");
Windows下调用系统命令
String [] cmd={"cmd","/C","copy file1.txt file2.txt"};
Process proc =Runtime.getRuntime().exec(cmd);
Linux下调用系统命令就要改成下面的格式
String [] cmd={"/bin/sh","-c","ln -s file1 file2"};
Process proc =Runtime.getRuntime().exec(cmd);
Windows下调用系统命令并弹出命令行窗口
String [] cmd={"cmd","/C","start copy file1.txt file2.txt"};
Process proc =Runtime.getRuntime().exec(cmd);
Linux下调用系统命令并弹出终端窗口就要改成下面的格式
String [] cmd={"/bin/sh","-c","xterm -e ln -s file1 file2"};
Process proc =Runtime.getRuntime().exec(cmd);
还有要设置调用程序的工作目录就要
Process proc =Runtime.getRuntime().exec("ls",null, new File("/bin"));
]]>
</screen>
<section id="system">
<title>System</title>
<screen>
<![CDATA[
Java.version 运行时环境版本
java.vendor 运行时环境供应商
java.vendor.url 供应商的 URL
java.home 安装目录
java.vm.specification.version 虚拟机规范版本
java.vm.specification.vendor 虚拟机规范供应商
java.vm.specification.name 虚拟机规范名称
java.vm.version 虚拟机实现版本
java.vm.vendor 虚拟机实现供应商
java.vm.name 虚拟机实现名称
java.specification.version 运行时环境规范版本
java.specification.vendor Java 运行时环境规范供应商
java.specification.name Java 运行时环境规范名称
java.class.version Java 类格式版本号
java.class.path Java 类路径
java.library.path 加载库时搜索的路径列表
java.io.tmpdir 默认的临时文件路径
java.compiler 要使用的 JIT 编译器的名称
java.ext.dirs 一个或多个扩展目录的路径
os.name 操作系统的名称
os.arch 操作系统的架构
os.version 操作系统的版本
file.separator 文件分隔符(在 UNIX 系统中是“/”)
path.separator 路径分隔符(在 UNIX 系统中是“:”)
line.separator 行分隔符(在 UNIX 系统中是“/n”)
user.name 用户的账户名称
user.home 用户的主目录
user.dir 用户的当前工作目录
]]>
</screen>
<section id="System">
<title>System</title>
<section>
<title>退出码</title>
<programlisting>
<![CDATA[
System.exit(0);
]]>
</programlisting>
</section>
<section>
<title>user.dir</title>
<programlisting>
<![CDATA[
public class Test {
public static void main(String[] args) {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("java.vendor.url"));
}
}
]]>
</programlisting>
</section>
<section>
<title>java.io.tmpdir</title>
<para>改变java.io.tmpdir的默认值</para>
<programlisting>
System.setProperty("java.io.tmpdir", "/vat/tmp");
System.out.println(System.getProperty("java.io.tmpdir"));
</programlisting>
<para>如果你希望从外部修改这个系统属性的话,你可以使用-D这个参数,例如 java
-Djava.io.tmpdir=/path/to/tmpdir</para>
<programlisting>
System.out.println(System.getProperty("java.io.tmpdir"));
</programlisting>
</section>
<section id="file.encoding">
<title>打印当前 Java 文件的默认编码</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.nio.charset.Charset;
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
System.out.println(System.getProperty("file.encoding"));
System.out.println(Charset.defaultCharset());
}
}
]]>
</programlisting>
</section>
<section>
<title>自定义</title>
<programlisting>
<![CDATA[
public static void main(String[] args) {
try {
Oracle oracle = new Oracle();
if(System.getProperty("config") == null){
System.exit(1);
}
oracle.openConfig(System.getProperty("config"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
]]>
</programlisting>
<para>上面程序编译打包后运行</para>
<screen>
<![CDATA[
neo@netkiller:~/project/Oracle$ java -jar -Dconfig=jdbc.properties target/oracle-0.0.1-SNAPSHOT.jar
]]>
</screen>
</section>
</section>
<section>
<title>System.in 标准输入(Stdin)</title>
<para>标准输入一般用于管道输入,例如:</para>
<screen>
<![CDATA[
cat test.txt | java cn.netkiller.ipo.test.StdinToStdout
]]>
</screen>
<para>下面的程序例子里从管道输入,并从标准输出打印。</para>
<programlisting>
<![CDATA[
package cn.netkiller.ipo.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StdinToStdout {
public StdinToStdout() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
String s = "";
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
while ((s = stdIn.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
]]>
</programlisting>
<para>默认 BufferedReader 缓冲区比较小,无法处理大于1000 行的输入,可以通过配置缓冲区来解决。</para>
<programlisting>
<![CDATA[
stdin = new BufferedReader(new InputStreamReader(System.in, "utf-8"), 1024 * 1024 * 1024); // 1G 缓冲区
]]>
</programlisting>
</section>
<section>
<title>ANSI Color</title>
<programlisting>
<![CDATA[
public class AnsiColorTest {
public static void main(String[] args) {
System.out.println("Hello,Neo!");
System.out.println("\033[30;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[31;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[32;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[33;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[34;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[35;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[36;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[37;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[40;31;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[41;32;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[42;33;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[43;34;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[44;35;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[45;36;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[46;37;4m" + "Hello,Neo!" + "\033[0m");
System.out.println("\033[47;4m" + "Hello,Neo!" + "\033[0m");
}
}
]]>
</programlisting>
</section>
</section>
</chapter>