java怎样调用外部程序,程序的名称中有空格

palm_civet 2006-08-17 04:51:20
rt

java怎样调用外部程序,程序的名称中有空格
...全文
195 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhmt 2006-08-17
  • 打赏
  • 举报
回复
无情接分,友情up!
kamhung 2006-08-17
  • 打赏
  • 举报
回复
参数当中有空格的话, 需要用引号引起来
kamhung 2006-08-17
  • 打赏
  • 举报
回复
如果要运行C://run test.bat
可以用 new ProcessBuilder("\"C:/run test.bat\"").start();
congliu 2006-08-17
  • 打赏
  • 举报
回复
刚好以前写过,调用windows的net send,已经包含空格,请参考

import java.util.*;
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 class GoodWindowsExec
{
public static void main(String args[])
{
int times=100;
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}

try
{ for(int i=0;i<=times;i++){
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];

if( osName.equals( "Windows 2000" ) )
{

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();
}
}
}
healer_kx 2006-08-17
  • 打赏
  • 举报
回复
Runtime.getRuntime().exec()

空格不是问题。看Java DOC

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧