看看这段有没有帮助: Runtime.class API Process exec(String command) Executes the specified string command in a separate process. Process exec(String[] cmdarray) Executes the specified command and arguments in a separate process. Process exec(String[] cmdarray, String[] envp) Executes the specified command and arguments in a separate process with the specified environment. Process exec(String[] cmdarray, String[] envp, File dir) Executes the specified command and arguments in a separate process with the specified environment and working directory. Process exec(String cmd, String[] envp) Executes the specified string command in a separate process with the specified environment. Process exec(String command, String[] envp, File dir) Executes the specified string command in a separate process with the specified environment and working directory. Process.class API abstract InputStream getErrorStream() Gets the error stream of the subprocess. abstract InputStream getInputStream() Gets the input stream of the subprocess. abstract OutputStream getOutputStream() Gets the output stream of the subprocess.
我的console程序比较复杂,运行它后还要和它交互:
smart.exe inter spec0run中国
.
q
这个ping也没什么复杂的啊代为注释:
//下面这句调用Runtime类,这个类可以得到jvm的相关参数,getRuntime()是静态方法
//exec命令就是执行一个外部程序.exec(String programname)
Process me=Runtime.getRuntime().exec("ping 202.204.120.68");//BufferedReader是java.io中的类,可以得到文本输入
//me.getInputStream就是把exec执行的命令的显示作为Input流,
//然后先用InputStreamReader读取
BufferedReader mein=new BufferedReader(new InputStreamReader(me.getInputStream()));String c;
//mein.readLine()就是从输出中读取一行
while(c=mein.readLine())!=null
{
out.println(c+"<br>");
}
得到的结果,用一个分析器来包装如果结果是固定的,每行代表固定意思就容易了
既然有了inputstream就可以处理每一行的结果了
如果是动态的,还需要在每一行作个关键字来标示,判断每行是干吗的
请回答一下zhaofj(叶三耿)的问题吧。
我的问题其实和他的类似。麻烦贴出代码,谢谢了。
env是environment,pathname是路径。
而不是在系统内部执行
也就是说打开一个CMD命令窗口
像平常在DOS下运行的一样
能否举例说明一下你的代码,让俺们菜鸟类学学
Runtime.class API
Process exec(String command)
Executes the specified string command in a separate process.
Process exec(String[] cmdarray)
Executes the specified command and arguments in a separate process.
Process exec(String[] cmdarray, String[] envp)
Executes the specified command and arguments in a separate process with the specified environment.
Process exec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Process exec(String cmd, String[] envp)
Executes the specified string command in a separate process with the specified environment.
Process exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory. Process.class API
abstract InputStream getErrorStream()
Gets the error stream of the subprocess.
abstract InputStream getInputStream()
Gets the input stream of the subprocess.
abstract OutputStream getOutputStream()
Gets the output stream of the subprocess.
那么就足够判断这个人是否登陆完成了吧?或许还能得到他输入的密码@_@没试过,仅是想法
问:
为什么Runtime.exec("ls")没有任何输出? 答: 调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例,该实例可用于控制进程或取得进程的相关信息. 由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO(如stdin,stdou,stderr)都通过Process.getOutputStream(),Process.getInputStream(), Process.getErrorStream()方法重定向给它的父进程了.用户需要用这些stream来向 子进程输入数据或获取子进程的输出. 所以正确执行Runtime.exec("ls")的例程如下: try
{
process = Runtime.getRuntime().exec (command);
InputStreamReader ir=newInputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader (ir); String line;
while ((line = input.readLine ()) != null) System.out.println(line);
} catch (java.io.IOException e){
System.err.println ("IOException " + e.getMessage());
}