Java Runtime.exec() 方法的陷阱与解决

需积分: 15 1 下载量 120 浏览量 更新于2024-09-12 收藏 47KB DOCX 举报
"这篇文档是关于Java中`Runtime.exec()`方法使用时可能遇到的问题的中文翻译版,主要探讨了该方法的四个潜在陷阱,并通过代码示例进行了详细解释。" 在Java编程中,`Runtime.exec()`是一个非常重要的方法,它允许程序在运行时执行操作系统级别的命令。然而,这个方法并不总是如预期般工作,存在一些常见的陷阱和问题。以下是文档中提到的四个陷阱的详细说明: 1. 非法线程状态异常(IllegalThreadStateException): 当尝试调用`Process`对象的`exitValue()`方法,而该进程尚未终止时,会抛出`IllegalThreadStateException`。例如,以下代码尝试执行`javac.exe`并立即获取退出值,但这可能会导致异常,因为编译器可能还在运行: ```java Process p = Runtime.getRuntime().exec("javac MyProgram.java"); int exitVal = p.exitValue(); // 这里可能抛出异常 ``` 应确保在调用`exitValue()`之前,进程已经完成。 2. 命令行参数的处理: `Runtime.exec()`接受字符串或字符串数组作为参数。如果使用单个字符串,命令和参数之间要用空格分隔,这可能导致解析问题。例如,如果命令需要包含带有空格的路径,必须使用字符串数组来正确传递: ```java String[] cmd = {"javac", "C:\\Program Files\\MyProject\\MyProgram.java"}; Process p = Runtime.getRuntime().exec(cmd); ``` 3. 环境变量的设置: `Runtime.exec()`还可以接收环境变量的数组,用于传递到新进程中。如果需要设置特定的环境变量,可以使用带环境变量参数的重载方法。例如: ```java String[] env = {"JAVA_HOME=C:\\Java\\JDK1.8"}; Process p = Runtime.getRuntime().exec(cmdArray, env); ``` 4. 标准输入/输出和错误流的处理: 执行外部命令时,可能会产生输出和错误。如果不处理这些流,它们可能会阻塞进程,导致程序挂起。因此,通常需要读取并关闭`Process`对象的`InputStream`和`ErrorStream`: ```java InputStream errStream = p.getErrorStream(); BufferedReader errReader = new BufferedReader(new InputStreamReader(errStream)); String line; while ((line = errReader.readLine()) != null) { System.err.println(line); } errReader.close(); ``` 了解并避免这些陷阱对于正确使用`Runtime.exec()`至关重要。在调用`exec()`时,确保考虑到进程的生命周期管理、命令参数的正确传递以及输入/输出流的处理,才能确保程序的稳定性和正确性。

MODULE ispmach TITLE 'simple alu' "program alu8_16.abl" DECLARATIONS clk pin 68; alu_f1,alu_f0,b_sel,oe pin 87..84; D7..D0 pin 24,23,26,25, 28,27,30,29; Y7..Y0 pin 32..39; carry,zero pin 80, 81; Acc_7..Acc_0 pin 77..70; B7..B0,A7..A0,a_sel node istype 'com'; c8..c1,c0 node istype 'com'; Acc7..Acc0 node istype 'reg,keep'; alu_f=[alu_f1,alu_f0]; A=[A7..A0]; B=[B7..B0]; D=[D7..D0]; Y=[Y7..Y0]; Acc=[Acc7..Acc0]; EQUATIONS when b_sel then B=Acc; else B=[0,0,0,0,0,0,0,0]; when alu_f==[0,1] then {c0=1;a_sel=1;} when a_sel then A=!D; else A=D; Acc:=Y; Acc.CLK=clk; [Acc_7..Acc_0]=Acc; [Acc_7..Acc_0].oe=oe; when (alu_f==[0,0])#(alu_f==[0,1]) then { Y0=B0&A0&c0 # B0&!A0&!c0 #!B0&A0&!c0 # !B0&!A0&c0; Y1=B1&A1&c1 # B1&!A1&!c1 #!B1&A1&!c1 # !B1&!A1&c1; Y2=B2&A2&c2 # B2&!A2&!c2 #!B2&A2&!c2 # !B2&!A2&c2; Y3=B3&A3&c3 # B3&!A3&!c3 #!B3&A3&!c3 # !B3&!A3&c3; Y4=B4&A4&c4 # B4&!A4&!c4 #!B4&A4&!c4 # !B4&!A4&c4; Y5=B5&A5&c5 # B5&!A5&!c5 #!B5&A5&!c5 # !B5&!A5&c5; Y6=B6&A6&c6 # B6&!A6&!c6 #!B6&A6&!c6 # !B6&!A6&c6; Y7=B7&A7&c7 # B7&!A7&!c7 #!B7&A7&!c7 # !B7&!A7&c7; c1=B0&A0 # B0&c0 # A0&c0; c2=B1&A1 # B1&c1 # A1&c1; c3=B2&A2 # B2&c2 # A2&c2; c4=B3&A3 # B3&c3 # A3&c3; c5=B4&A4 # B4&c4 # A4&c4; c6=B5&A5 # B5&c5 # A5&c5; c7=B6&A6 # B6&c6 # A6&c6; c8=B7&A7 # B7&c7 # A7&c7; } when alu_f==[1,0] then Y=B&A; when alu_f==[1,1] then { Y0=B0#A0; Y1=B1#A1; Y2=B2#A2; Y3=B3#A3; Y4=B4#A4; Y5=B5#A5; Y6=B6#A6; Y7=B7#A7; } when (alu_f==[0,0]) then carry= c8; when (alu_f==[0,1]) then carry=!c8; when [Y7..Y0]==^h00 then zero=1; END

2023-06-11 上传

"program alu8_16.abl 2023/5/17 DECLARATIONS clk pin 68; alu_f1,alu_f0,b_sel,oe pin 87..84; D7..D0 pin 24,23,26,25,28,27,30,29; Y7..Y0 pin 32..39; carry,zero pin 80,81; Acc_7..Acc_0 pin 77..70; B7..B0,A7..A0,a_sel node istype'com'; c8..c1,c0 node istype'com'; Acc7..Acc0 node istype'reg,keep'; alu_f=[alu_f1,alu_f0]; A=[A7..A0]; B=[B7..B0]; D=[D7..D0]; Y=[Y7..Y0]; Acc=[Acc7..Acc0]; EQUATIONS when b_sel then B=Acc; else B=[0,0,0,0,0,0,0,0]; when alu_f==[0,1] then {c0=1;a_sel=1;} when a_sel then A!=D; else A=D; Acc:=Y; Acc.CLK=clk; [Acc_7..Acc_0]=Acc; [Acc_7..Acc_0].oe=oe; when(alu_f==[0,0])#(alu_f==[0,1]) then { Y0=B0&A0&C0 # B0&!A0&!c0 # !B0&A0&!c0 # !B0&!A0&c0; Y1=B1&A1&C1 # B1&!A1&!c1 # !B1&A1&!c1 # !B1&!A1&c1; Y2=B2&A2&C2 # B2&!A2&!c2 # !B2&A2&!c2 # !B2&!A2&c2; Y3=B3&A3&C3 # B3&!A3&!c3 # !B3&A3&!c3 # !B3&!A3&c3; Y4=B4&A4&C4 # B4&!A4&!c4 # !B4&A4&!c4 # !B4&!A4&c4; Y5=B5&A5&C5 # B5&!A5&!c5 # !B5&A5&!c5 # !B5&!A5&c5; Y6=B6&A6&C6 # B6&!A6&!c0 # !B6&A6&!c6 # !B6&!A6&c6; Y7=B7&A7&C7 # B7&!A7&!c7 # !B7&A7&!c7 # !B7&!A7&c7; c1=B0&A0 # B0&c0 # A0&c0; c2=B1&A1 # B1&c1 # A1&c1; c3=B2&A2 # B2&c2 # A2&c2; c4=B3&A3 # B3&c3 # A3&c3; c5=B4&A4 # B4&c4 # A4&c4; c6=B5&A5 # B5&c5 # A5&c5; c7=B6&A6 # B6&c6 # A6&c6; c8=B7&A7 # B7&c7 # A7&c7; } when alu_f==[1,0] then Y=B&A; when alu_f==[1,1] then { Y0=B0#A0; Y1=B1#A1; Y2=B2#A2; Y3=B3#A3; Y4=B4#A4; Y5=B5#A5; Y6=B6#A6; Y7=B7#A7; } when (alu_f==[0,0]) then carry=c8; when (alu_f==[0,1]) then carry=!c8; when ([Y7..Y0]==^h00) then zero=1; END这段语言每一句的作用及原理

2023-05-23 上传

select t.id ,t.parent, t.name ,t.begin ,t.end , t.ACTUAL_START , t.ACTUAL_FINISH, t.TASK_UNIQUE_NO, t.NO, t.SUMMARY, t.DEPENDENCE, t.PRIORITY, t.EXEC_STAT, t.DURATION, t.COMP_PCT,ASSIGNER,POSITION,PRINCIPAL,PRINCIPAL_NAME,ORG_NAME,MGR_LINE,ERJIGUANXIAN,SFSJYS,SFNDJH, t.CRITICAL,t.PROJ_NO,t.SRC_TID,t.ASSIGNER_AUTH,t.POSITION_NAME,t.ASSIGNER_NAME,t.PRIN_ORG,t.ORG,t.SRC_SYS,t.CREATE_USER, t.TASK_NO,tp.id as typ,tp.name as typname,t.SETTLETYPECODE as SETTLETYPECODE,'' as remark,'' as type,t.OATASKID as OATASKID,t.QIQU,t.DESCRIBE, S.DESCRIPTION,ontheway_type,T.SHIFOUXXJDHB,up.update_date optime,submit,t.AUDITOR,t.AUDITOR_NO,t.proj_name,tp.filter_type,t.plan,t.MATTER_SRC,t.Prepose_task,t.shixiangbiaoqian, t.SETTLE_LEVEL,tp.specail_name,t.audit_unit,t.rectify_question_type,t.project_data_processor,t.project_data_processor_name, CASE WHEN t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and ( up.STAT = 'OP' or up.STAT = 'I') then 'relay' when t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and up.STAT = 'U' and up.FINISH ='Y' and up.rel = '1' AND up.RID is not null then 'finish' when t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and up.STAT = 'U' and up.DELAY ='Y' and up.rel = '1' AND up.RID is not null then 'delay' when t.EXEC_STAT not IN ('30','31','32','33','34','41','55') and up.STAT = 'U' and up.TERMINATE ='P' and up.rel = '1' AND up.RID is not null then 'terminate' else null end as approve_type, up.user_id as approver from t_master_task t left join t_task_2_task_typ p on t.id = p.task left join t_task_typ tp on tp.id = p.typ left join t_slave_task s on s.id=t.id left join (select * from (select a.*,row_number() over(partition by task order by update_date desc) rm from t_user_task_upd_stat a) where rm = 1) up on t.id = up.task; 这sql怎么优化

2023-02-07 上传