已经取得runtime(),执行exec时,没有出现错误,但是也没有执行结果。
请指教,thx
请指教,thx
解决方案 »
- 编写一个Java应用程序,使用 RandomAccessFile流统计 Hello.txt中的单词
- JOptionPane.showMessageDialog(this,"You input number is Wrong!!!(0~100)","Error",JOptionPane.ERROR_MESSAGE);问题
- 左边jtree右边jtable的资源管理器问题
- 一个很妖的问题(sun公司的面试题)
- 求Spring API文档
- 如何用java对端口进行扫描?
- 编译dll的问题
- struts是什么东东?请教!
- 我这段程序出什么问题了?!请高手赐教!:(
- 下面代码编译不能通过,提示 'class' or 'interface' expected;多谢
- 菜鸟问题:为什么这样抛弃异常不行的啊
- 有没有办法设置Connection永远不会超时?
Runtime.getRuntime().exec("cmd /c copy 1.txt 2.txt");你执行的是什么?
以下是源码:
执行非dos类命令,例如javaimport java.util.*;
import java.io.*;public class MediocreExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
-----------------------------------
执行dos,windows命令: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[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}
try
{
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
System.out.println( osName);
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();
}
}
}
什么是dos和非dos的exe?
但是java,javac等就是与dos无关的命令