使用“进程”构建器在java中运行命令时出现问题。在测试机器中它可以正常工作但是一旦在服务器上,命令打开的进程冻结并且永不退出。此代码用于apache tomcat windows web服务器:
//WORD is the absolute path for msWord and inF is the absolute path
//for the file to save as pdf
public boolean changeFormatMSOffice(String inF, String WORD) {
System.out.println("changeFormatMSOffice(" + inF + "," + WORD + ")");
String macro = "";
ArrayList<String> wordArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_WORD.replace(" ", "").split(",")));
ArrayList<String> excelArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_EXCEL.replace(" ", "").split(",")));
ArrayList<String> ppArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_PP.replace(" ", "").split(",")));
String extension = inF.substring(inF.lastIndexOf(".")).replace(".", "").trim();
BufferedWriter out;
List<String> cmdList = new ArrayList<String>();
cmdList.add(WORD);
String saveFile = "";
if (wordArr.contains(extension)) {
macro = "/msaveAsPDF";
cmdList.add(macro);
cmdList.add(inF);
} else if (excelArr.contains(extension) || ppArr.contains(extension)) {
if (excelArr.contains(extension)) {
macro = "/mSaveXLSAsPDF";
} else {
macro = "/msavePPTAsPDF";
}
cmdList.add(macro);
int fileNum = 0;
saveFile = "\"" + PATH + (PATH.substring(PATH.length() - 1).equals(File.separator) ? "" : File.separator) + fileNum + ".txt\"";
while (new File(saveFile).exists()) {
fileNum++;
saveFile = "\"" + PATH + (PATH.substring(PATH.length() - 1).equals(File.separator) ? "" : File.separator) + fileNum + ".txt\"";
}
try {
out = new BufferedWriter(new FileWriter(saveFile));
out.write(inF);
out.close();
cmdList.add(saveFile);
} catch (Exception e) {
System.err.println(e.toString());
}
}
try {
ProcessBuilder proc = new ProcessBuilder(cmdList);
System.out.println("PreWaitForList");
Process pro = proc.start(); //<----- important part starts here
StreamGobbler g1 = new StreamGobbler("stdin", pro.getInputStream());
StreamGobbler g2 = new StreamGobbler("stderr", pro.getErrorStream());
g1.start();
g2.start();
pro.waitFor();//<---- hangs here, but need to wait for it to exit(system requirement)
System.out.println("AfterWaitFor");
try {
if (!saveFile.equals("")) {
new File(saveFile).delete();
}
} catch (Exception e) {
}
return true;
} catch (Exception e) {
System.err.println(e.toString());
return false;
}
}
我发现信息说你需要收集std和错误流,否则它将会解放,所以我使用了以下流gobbler实现:
public class StreamGobbler implements Runnable {
String name;
InputStream is;
Thread thread;
public StreamGobbler(String name, InputStream is) {
this.name = name;
this.is = is;
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine();
if (s == null) {
break;
}
System.out.println("[" + name + "] " + s);
}
is.close();
} catch (Exception ex) {
System.out.println("Problem reading stream " + name + "... :" + ex);
ex.printStackTrace();
}
}
}
请注意,在命令行中运行命令时,它在测试计算机和服务器中都可以正常工作
答案 0 :(得分:1)
从java vs命令行启动进程时,环境参数(如$ path)和工作目录是否相同?