锘??xml version="1.0" encoding="utf-8" standalone="yes"?> import java.util.*; import java.io.*; public class BadExecJavac { public static void main(String args[]) { try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("javac"); int exitVal = proc.exitValue(); System.out.println("Process exitValue: " + exitVal); } catch (Throwable t) { t.printStackTrace(); } } } 榪愯緇撴灉濡備笅 榪欐槸鍥犱負(fù)褰撹繘紼嬭繕娌℃湁緇撴潫鐨勬儏鍐典笅錛岃皟鐢╡xitValue鏂規(guī)硶浼?xì)鎶涘嚭IllegalThreadStateException.褰撶劧浜?jiǎn)鎴戜滑浼?xì)闂負(fù)浠鍚楄繖涓柟娉曚笉浼?xì)绛夊埌杩浗E嬬粨鏉熷湪榪斿洖涓涓悎鐞嗙殑鍊鹼紵 鍦ㄦ鏌rocess綾葷殑鎵鏈夊彲鐢ㄦ柟娉曚互鍚庢垜浠彂鐜癢airFor()鏄竴涓洿鍚堥傜殑鏂規(guī)硶銆備簨瀹炰笂waitFor涔熶細(xì)榪斿洖exit value銆傝繖鎰忓懗鐫浣犱笉鍙互鍚屾椂鐢╡xitvalue鍜寃aitfor錛岃屾槸鍙兘閫夋嫨涓涓?/p> 褰撶劧浜?jiǎn)涔熸湁鎯呭喌浣犺鍦╳aitfor涔嬪墠鐢╡xitvalue鏂規(guī)硶錛氬氨鏄綘涓嶆兂鍥犱負(fù)澶栭儴紼嬪簭姘歌繙鏃犳硶瀹屾垚鑰屼竴鐩寸瓑寰呬笅鍘匯?/p> 鍥犳涓轟簡(jiǎn)閬垮厤榪欎釜闄烽槺錛屾垜浠涔堟崟鑾稩llegalThreadStateException寮傚父錛岃涔堢瓑寰呰繘紼嬪畬鎴愩傛垜浠浉褰撶劧鐨勪互涓哄彲浠ョ敤waitfor鏉ョ瓑寰呯▼搴忕殑緇撴潫銆備唬鐮佸涓嬶細(xì) 榪欐鍦╨inux涓嬮潰榪斿洖鐨勭粨鏋滄槸2,鑰屽湪windows涓嬮潰鎹紼嬪簭浼?xì)鎸傝跌P紝鍏充簬鍏跺師鍥犳垜浠彲浠ュ湪jdk鏂囨。涓壘鍒伴儴鍒嗚В閲婏細(xì)鍥犱負(fù)涓浜涙搷浣滅郴緇熶負(fù)鏍囧噯鐨勮緭鍏ヨ緭鍑轟粎浠呮彁渚涙湁闄愮殑緙撳啿鍖猴紝褰撲笉鑳芥紜殑灝嗕俊鎭啓榪涜緭鍏ユ祦鎴栬呬粠杈撳嚭嫻佷腑鑾峰彇淇℃伅鏃訛紝灝變細(xì)瀵艱嚧瀛愯繘紼嬬殑闃誨錛岀敋鑷蟲閿併傜幇鍦ㄦ垜浠氨鏍規(guī)嵁jdk鏂囨。鏉ュ鐞唈avac榪涚▼鐨勮緭鍑猴紝褰撲綘涓嶅甫浠諱綍鍙傛暟榪愯javac鏃訛紝瀹冧細(xì)鎵撳嵃鍑轟竴緋誨垪鐨勬湁鐢ㄧ殑鎻愮ず淇℃伅銆傝岃繖浜涗細(xì)琚紶閫佸埌stderr嫻佷腑銆傛垜浠彲浠ュ啓紼嬪簭鍦ㄥ叾榪斿洖鍓嶈幏鍙栬繖浜涗俊鎭備笅闈㈢殑浠g爜鎻愪緵浜?jiǎn)涓涓鉤搴哥殑瑙e喅鏂規(guī)銆?/p>import java.util.*;import java.io.*; 榪欐紼嬪簭鍙互姝g‘鐨勮緭鍑轟簡(jiǎn)鎻愮ず淇℃伅錛屼絾鏄垜浠簲璇ユ敞鎰忓埌鍏惰繑鍥炰唬鐮佹槸2錛屾垜浠煡閬撲換浣曢潪0鐨勮繑鍥炰唬鐮侀兘琛ㄧず紼嬪簭涓嶆甯搞傛墍浠ユ垜浠渶瑕佽繘涓姝ョ殑鏌ユ壘鍘熷洜銆傚浜巜in32鑰岃█鏄痜ile not found錛屽緢鏄庢樉javac鏈熸湜鎴戜滑鎻愪緵緙栬瘧鐨勬枃浠躲傛墍浠ュ浜庢案榪滄寕璧風(fēng)殑闂錛屽鏋滀綘榪愯鐨勭▼搴忎細(xì)鏈夎緭鍑烘垨鑰呰姹傝緭鍑哄叆鏃訛紝浣犻渶瑕佸鐞嗚緭鍑哄拰杈撳叆銆傛垜鍦╨inux涓嬮潰榪愯鐨勭粨鏋滄槸姝g‘鐨勩傚墠闈㈣浜?jiǎn)鍦╳in32涓嬮潰2浠h〃鏄枃浠舵病鏈夋壘鍒幫紝鑰屽湪榪欑鎯呭喌涓嬭〃鏄庢槸dir.exe娌℃湁鎵懼埌錛岋紙鍥犱負(fù)鏍規(guī)湰灝辨病鏈夎繖涓枃浠訛紝浠栦滑閮借灝佽鍒癱ommon.com 錛坵in95錛夋垨鑰卌md.exe涓簡(jiǎn)銆?/p> 涓嬮潰鎴戜滑鍒楀嚭涓涓紜殑澶勭悊Process鐨勮緭鍏ヨ緭鍑烘祦鐨勬柟娉曘傞渶瑕佺敤涓涓嚎紼嬬被銆?/p> import java.util.*; 鐢ㄤ簬涓撻棬鐨勫鐞嗚緭鍏ヨ緭鍑恒?/p> public class GoodWindowsExec{ 濡傛灉榪愯濡備笅鍛戒護(hù)涓婇潰鐨勪唬鐮佷細(xì)璋冪敤word紼嬪簭 >java GoodWindowExec “abc.doc” 涔熷氨鏄鏂囦歡綾誨瀷濡傛灉window鑳藉璇嗗埆瀹冨氨浼?xì)璋冪敤瀵瑰簲鐨劷E嬪簭澶勭悊銆?/p> StreamGlobbler鐨勬渶閲嶈浣滅敤鏄粬浼?xì)娓吔I烘墍鏈夌殑浼犻掔粰浠栫殑inputstream錛岃繖鏍蜂笉浼?xì)閫犳垚Process闃誨鎴栬呮閿併?/p>
java.lang.IllegalThreadStateException: process has not exited
at java.lang.Win32Process.exitValue(Native Method)
at BadExecJavac.main(BadExecJavac.java:13)
import java.io.*;
public class BadExecJavac2{
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public class MediocreExecJavac{
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
import java.io.*;
class StreamGobbler extends Thread{
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}
try {
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows NT" ) ) {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
} else if( osName.equals( "Windows 95" ) ) {
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = args[0]; }
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
Process proc = rt.exec(cmd); // any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); // any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); // kick them off
errorGobbler.start();
outputGobbler.start(); // any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
]]>
浠ヤ笂閫昏緫綆鍗曠殑璇村氨鏄細(xì)濡傛灉鏉′歡涓嶆弧瓚籌紝鍒欑瓑寰呫傚綋鏉′歡婊¤凍鏃訛紝絳夊緟璇ユ潯浠剁殑綰跨▼灝嗚鍞ら啋銆傚湪Java涓紝榪欎釜鏈哄埗鐨勫疄鐜頒緷璧栦簬wait/notify銆傜瓑寰呮満鍒朵笌閿佹満鍒舵槸瀵嗗垏鍏寵仈鐨勩備緥濡傦細(xì)
synchronized(obj) {while(!condition) {obj.wait();}obj.doSomething();}
褰撶嚎紼婣鑾峰緱浜?jiǎn)obj閿佸悗錛屽彂鐜版潯浠禼ondition涓嶆弧瓚籌紝鏃犳硶緇х畫涓嬩竴澶勭悊錛屼簬鏄嚎紼婣灝眞ait()銆?/span>
鍦ㄥ彟涓綰跨▼B涓紝濡傛灉B鏇存敼浜?jiǎn)鏌愪簺鏉′挥灱屼娇寰椌U跨▼A鐨刢ondition鏉′歡婊¤凍浜?jiǎn)锛尀鍙互鍞ら啋绾拷E婣錛?/span>
synchronized(obj) {condition = true;obj.notify();}
闇瑕佹敞鎰忕殑姒傚康鏄細(xì)
◆璋冪敤obj鐨剋ait(), notify()鏂規(guī)硶鍓嶏紝蹇呴』鑾峰緱obj閿侊紝涔熷氨鏄繀欏誨啓鍦╯ynchronized(obj) {...} 浠g爜孌靛唴銆?/span>
◆璋冪敤obj.wait()鍚庯紝綰跨▼A灝遍噴鏀句簡(jiǎn)obj鐨勯攣錛屽惁鍒欑嚎紼婤鏃犳硶鑾峰緱obj閿侊紝涔熷氨鏃犳硶鍦╯ynchronized(obj) {...} 浠g爜孌靛唴鍞ら啋A銆?/span>
◆褰搊bj.wait()鏂規(guī)硶榪斿洖鍚庯紝綰跨▼A闇瑕佸啀嬈¤幏寰梠bj閿侊紝鎵嶈兘緇х畫鎵ц銆?/span>
◆濡傛灉A1,A2,A3閮藉湪obj.wait()錛屽垯B璋冪敤obj.notify()鍙兘鍞ら啋A1,A2,A3涓殑涓涓紙鍏蜂綋鍝竴涓敱JVM鍐沖畾錛夈?/span>
◆obj.notifyAll()鍒欒兘鍏ㄩ儴鍞ら啋A1,A2,A3錛屼絾鏄緇х畫鎵цobj.wait()鐨勪笅涓鏉¤鍙ワ紝蹇呴』鑾峰緱obj閿侊紝鍥犳錛孉1,A2,A3鍙湁涓涓湁鏈轟細(xì)鑾峰緱閿佺戶緇墽琛岋紝渚嬪A1錛屽叾浣欑殑闇瑕佺瓑寰匒1閲婃斁obj閿佷箣鍚庢墠鑳界戶緇墽琛屻?/span>
◆褰揃璋冪敤obj.notify/notifyAll鐨勬椂鍊欙紝B姝f寔鏈塷bj閿侊紝鍥犳錛孉1,A2,A3铏借鍞ら啋錛屼絾鏄粛鏃犳硶鑾峰緱obj閿併傜洿鍒癇閫鍑簊ynchronized鍧楋紝閲婃斁obj閿佸悗錛孉1,A2,A3涓殑涓涓墠鏈夋満浼?xì)鑾峰緱閿伨lх畫鎵ц銆?/span>
瀹夎濂界殑CentOS浼?xì)鑷甫OpenJdk,鐢ㄥ懡浠?java -version 錛屼細(xì)鏈変笅闈㈢殑淇℃伅錛?/p>
java version "1.6.0"
OpenJDK Runtime Environment (build 1.6.0-b09)
OpenJDK 64-Bit Server VM (build 1.6.0-b09, mixed mode)
鏈濂借繕鏄厛鍗歌澆鎺塷penjdk,鍦ㄥ畨瑁卻un鍏徃鐨刯dk.
鍏堟煡鐪?rpm -qa | grep java
鏄劇ず濡備笅淇℃伅錛?/p>
java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
鍗歌澆錛?/p>
rpm -e --nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
榪樻湁涓浜涘叾浠栫殑鍛戒護(hù)
rpm -qa | grep gcj
rpm -qa | grep jdk
濡傛灉鍑虹幇鎵句笉鍒皁penjdk source鐨勮瘽錛岄偅涔堣繕鍙互榪欐牱鍗歌澆
yum -y remove java java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
yum -y remove java java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
<1># rpm -qa|grep jdk ← 鏌ョ湅jdk鐨勪俊鎭垨鐩存帴鎵ц
鎴?nbsp;
# rpm -q jdk
鎴?nbsp;
# java -version
<2># rpm -qa | grep gcj ← 紜gcj鐨勭増鏈彿
<3># yum -y remove java-1.4.2-gcj-compat ← 鍗歌澆gcj
絎簩姝ワ細(xì)瀹夎JDK
<1>浠嶴UN涓嬭澆jdk-1_5_0_14-linux-i586-rpm.bin鎴杍dk-1_5_0_14-linux-i586.bin
鍦?usr涓嬫柊寤簀ava鏂囦歡澶癸紝灝嗗畨瑁呭寘鏀懼湪/usr/java鐩綍涓?nbsp;
# mkdir /usr/java
<2>瀹夎JDK
# cd /usr/java
①jdk-1_5_0_14-linux-i586-rpm.bin鏂囦歡瀹夎
# chmod 777 jdk-1_5_0_14-linux-i586-rpm.bin ← 淇敼涓哄彲鎵ц
# ./jdk-1_5_0_14-linux-i586-rpm.bin ← 閫夋嫨yes鍚屾剰涓婇潰鐨勫崗璁?nbsp;
# rpm -ivh jdk-1_5_0_14-linux-i586.rpm ← 閫夋嫨yes鐩村埌瀹夎瀹屾瘯
②jdk-1_5_0_14-linux-i586.bin鏂囦歡瀹夎
# chmod a+x jdk-1_5_0_14-linux-i586.bin ← 浣垮綋鍓嶇敤鎴鋒嫢鏈夋墽琛屾潈闄?nbsp;
# ./jdk-1_5_0_14-linux-i586.bin ← 閫夋嫨yes鐩村埌瀹夎瀹屾瘯
絎笁姝ワ細(xì)閰嶇疆鐜鍙橀噺
<1># vi /etc/profile
<2>鍦ㄦ渶鍚庡姞鍏ヤ互涓嬪嚑琛岋細(xì)
export JAVA_HOME=/usr/java/jdk1.6.0_10
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
<3># reboot ← 閲嶅惎鏈哄櫒閰嶇疆鐢熸晥