我正在使用他们自己的参数启动我的jar 3程序。 第一和第二个程序完成它们的工作,但不是第二个程序。
这里是代码
Runtime rt = Runtime.getRuntime();
String cmdGet= "something";
try {
infolog("Executing command: " + cmdGet);
rt.exec(cmdGet);
}
catch(Exception ex){
infolog("Unable to launch program 2");
saveLog();
}
这里是命令
D:\Root\Module\Translators\L2Fo\SE_Draft_Update.exe -se_file="F:\Disp\Stage\DC\Ude34ea24x591ecb1c412x\M8486.dft" -input_file="F:\Disp\Stage\DC\Ude34ea24x591ecb1c412x\result\AttributeFile_M8486.txt" -log_file="F:\Disp\Stage\DC\Ude34ea24x591ecb1c412x\result\SELogg_M8486.txt"
在日志中我发现正确的命令已启动,但我发现它的工作没有完成。因此,通过在cmd提示符中手动复制字符串,它可以正常工作
此后的第三个程序也有效。 我不明白为什么。
答案 0 :(得分:0)
有时命令需要自己的cmd-shell。 然后你必须添加一个“CMD.EXE / C”前缀。这就是原因所在 'something'行在cmd-Window中工作正常。
if (b_shell == true) cmd = "CMD.EXE /C " + cmdstr;
else cmd = cmdstr;
...
rt.exec(cmd);
我将b_shell作为参数传递给包装器方法:
public static void systemCall(String cmdstr,boolean b_shell,Path logfile) throws IOException
答案 1 :(得分:0)
对不起那个答案,也许你想试试一个版本 能够等待终止命令并将命令的输出作为ArrayList返回。
/**
* Executes command 'cmdstr'. The output of the command is stored in a ArrayList-String array
* @param cmdstr The command line to be executed not containing a redirect to an outputfile ( > x.txt )
* @param b_wait If true the command waits until it is finished and 'errmsg' contains the exit value
* @param b_shell If true the command will be started in a shell (uses prefix CMD.EXE /C)
* @param errmsg OUT: Error message
* @return ArrayList-String output or null on error, see errmsg
* @since Last change: 2014.04.30
*/
public static ArrayList<String> systemCallOutputArray(String cmdstr,boolean b_wait,boolean b_shell,/*IO*/StringBuffer errmsg)
{
final String fn="systemCallOutputArray()";
errmsg.setLength(0);
if (cmdstr == null || cmdstr.isEmpty()) { errmsg.append(fn + ": Invalid arg. 'cmdstr' (null or empty)"); return null; }
ArrayList<String> output = new ArrayList<String>();
String cmd ="";
try
{
int exit_value = -1;
Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd);
String line="";
if (b_wait == true)
{
p.waitFor(); // waits until prozess is terminated, throws InterruptedException if wait is interrupted
exit_value = p.exitValue();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
{ if (line.isEmpty() == false) output.add(line);
}
input.close();
return output;
}
// the following lines are for b_wait = false
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
{ if (line.isEmpty() == false) output.add(line);
}
input.close();
return output;
}
catch (Exception e)
{
errmsg.append(fn + " failed: " + e.toString() + " cmd: '" + cmd + "'");
return null;
}
} //---------------- end of systemCallOutputArray()
答案 2 :(得分:0)
愿你试试这个:
public class testProcess {
static String cmdGet = "D:\\Root\\Module\\Translators\\L2Fo\\SE_Draft_Update.exe -se_file=\"F:\\Disp\\Stage\\DC\\Ude34ea24x591ecb1c412x\\M8486.dft\" "
+ "-input_file=\"F:\\Disp\\Stage\\DC\\Ude34ea24x591ecb1c412x\\result\\AttributeFile_M8486.txt\" "
+ "-log_file=\"F:\\Disp\\Stage\\DC\\Ude34ea24x591ecb1c412x\\result\\SELogg_M8486.txt\"";
public static void main(String[] args) {
run();
}
public static void run() {
try {
Process p = Runtime.getRuntime().exec(cmdGet);
} catch (Exception e) {
e.printStackTrace();
}
}
}