我正在编写一个Java服务器进程,该进程还应仅在Windows中运行带有参数的vb.exe文件。
我尝试将ProcessBuilder与start函数一起使用,将Process与exec函数一起使用,但是我没有错误,但是什么也没发生!
例如,cmd:
"C:\AL500\BIAFLABEL\AddToQueue.exe" "C:\AL500\BiafLabel\Templates\2.xml" -printer \\mickaelbpc\System-N
代码中的命令行定义:
String fullcmd = "\"C:\\AL500\\BIAFLABEL\\AddToQueue.exe\" \"C:\\AL500\\BiafLabel\\Templates\\2.xml\" -printer \\\\mickaelbpc\\System-N";
String fullcmd1 = "C:\\AL500\\BIAFLABEL\\AddToQueue.exe C:\\AL500\\BiafLabel\\Templates\\2.xml -printer \\\\mickaelbpc\\System-N";
String cmd1 = "C:\\AL500\\BIAFLABEL\\AddToQueue.exe";
String cmd2 = "C:\\AL500\\BiafLabel\\Templates\\2.xml";
String cmd3 = "-printer";
String cmd4 = "\\\\mickaelbpc\\System-N";
String[] command = new String[]{cmd1, cmd2, cmd3,cmd4};
进程+数组:
File dir = new File("C:/workspace");
Process process = Runtime.getRuntime().exec(command, null, dir);
process.waitFor();
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();
String strData;
StringBuffer sb = new StringBuffer("");
BufferedReader brData = new BufferedReader(new
InputStreamReader(stdout));
while ((strData = brData.readLine()) != null)
{
sb = sb.append(strData).append("\r\n");
}
brData.close();
ProcessBuilder +带有“:”的字符串命令:
ProcessBuilder pb=new ProcessBuilder(fullcmd);
pb.redirectErrorStream(true);
Process process1=pb.start();
BufferedReader inStreamReader = new BufferedReader(
new InputStreamReader(process1.getInputStream()));
String line;
while (true) {
line = inStreamReader.readLine();
if (line == null) { break; }
System.out.println(line);
ProcessBuilder +字符串命令,不带“:
File log = new File("log");
ProcessBuilder pb=new ProcessBuilder(/*command*/fullcmd1);
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process process1=pb.start();
BufferedReader inStreamReader = new BufferedReader(
new InputStreamReader(process1.getInputStream()));
String line;
while (true) {
line = inStreamReader.readLine();
if (line == null) { break; }
System.out.println(line);
}
更改后,我得到此错误:“ java.io. IOException :无法运行程序” C:\ AL500 \ BIAFLABEL \ AddToQueue.exe C:\ AL500 \ BiafLabel \ Templates \ 2.xml -printer \ mickaelbpc \ System-N“: CreateProcess error = 2 ,系统找不到指定的文件”
具有cmd.exe的ProcessBuilder:
ProcessBuilder pb=new ProcessBuilder("cmd.exe","/c",fullcmd);
pb.redirectErrorStream(true);
Process process1=pb.start();
BufferedReader inStreamReader = new BufferedReader(
new InputStreamReader(process1.getInputStream()));
String line;
while (true) {
line = inStreamReader.readLine();
if (line == null) { break; }
System.out.println(line);
}
我做了所有的选择,还有更多...如果有必要,我会添加更多示例
vb exe应打印一个文件。任何想法如何从Java进程运行它?或我的代码有什么问题?
答案 0 :(得分:0)
研究命令的错误和输出流。需要重定向它们并将它们流式传输到单独的线程中。或者尝试一下这个
文件日志=新文件(“日志”); pb.redirectErrorStream(true); pb.redirectOutput(Redirect.appendTo(log));
完整路径是否正确?您可以将逗号分隔的行空间打印到一个名为run.cmd的文件中,然后手动运行该行并查看提示符后会发生什么吗?
您要从哪个目录启动进程,可能与运行Java程序的目录不同?流程构建器有一个https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#directory()
要知道您的Java程序正在使用哪个目录,请执行以下操作:
File f = new File("./");
try{
System.out.println("Start dir is :" + f.getCanonicalFile());
}catch...
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#getCanonicalFile()
如果您重定向到日志,请参见github.com/tgkprog/nli/blob/master/RunCmd.java,不要在循环中再次获取流。并且不要通过cmd.exe调用。在我的示例中,忽略实际的命令,输入ur exe和params,我只是在ubuntu上调用了sh文件。您使用自己的4个参数进行调用