java怎样调用外部程序,程序的名称中有空格 rtjava怎样调用外部程序,程序的名称中有空格 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Runtime.getRuntime().exec()空格不是问题。看Java DOC 刚好以前写过,调用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(); } }} 如果要运行C://run test.bat可以用 new ProcessBuilder("\"C:/run test.bat\"").start(); 对象赋值疑问 java 调用存储过程的一些问题 这段代码怎么运行没有结果啊!! org.w3c.dom 一个java线形查找的例子 Format变量提示未定义,请问缺少了什么头文件 如何让一个文本区覆盖一个面板呢?(挺郁闷的) 新手初学Java,写一段程序大家给评价一下,顺便测试一下你和你的他(她)有多配(不许扔砖头啊) 如何把java.math.BigDecimal类型后面多余的零(0)去掉? 请问applet到底有哪些用途? 请问怎么使用反射机制 关于imageio的java.lang.OutOfMemoryError问题
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();
}
}
}
可以用 new ProcessBuilder("\"C:/run test.bat\"").start();