怎么在java里调用系统命令或者自己写的exe文件呢 我想,可能是因为JVM只能产生一个 console 窗口,并且只能由一个线程对它进行控制。因为我试过用 applet 来这样做的话,它是会产生 console 窗口并正常工作的。等下面的回答…… 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Process p = Runtime.getRuntime().exec(cmd, null); String buffer; InputStream pin = p.getInputStream(); InputStreamReader cin = new InputStreamReader(pin); BufferedReader in = new BufferedReader(cin); try { buffer = in.readLine(); while (buffer != null) { System.out.println(buffer); buffer = in.readLine(); } in.close(); } catch (IOException e) { e.printStackTrace(); } 报告:使用“cmd /c dir”仍然没有结果而按照nkwj的方法,如果使用自己的exe会出现“16位MS-DOS子系统”错误对话框说:c:\winnt\system32\ntvdm.exe设置应用程序环境时出错。选择“关闭”终止应用程序。这个错误跟直接调用Runtime.getRun.exec(cmd)一样。如果调用“cmd dir”会停止响应 谢谢各位解答,我想做的其实就是用java调用可执行程序,并且在java程序中取得执行结果。 import java.io.*;public class CmdTest{public static void main(String[] args) throws IOException{Process proc = Runtime.getRuntime().exec("notepad.exe");}} Process proc = Runtime.getRuntime().exec("cmd /E:ON /c start MyDocument.doc");在上面,被执行的命令是start MyDocument.doc,开关E:ON 指定DOS 命令处理器允许命令扩展,而开关/C 指明后面跟随的字符串是命令,并在执行命令后关闭DOS 窗口,start命令会开启一个单独的窗口执行所提供的命令。前一个答复中的“notepad.exe”必须式在系统变量path下 Process process = Runtime.getRuntime().exec("cmd /c dir"); System.out.println(loadStream(process.getInputStream())); //load the stream System.out.println(loadStream(process.getErrorStream())); //load the stream static String loadStream(InputStream in) throws IOException { int ptr = 0; in = new BufferedInputStream(in); StringBuffer buffer = new StringBuffer(); while ( (ptr = in.read()) != -1) { buffer.append( (char) ptr); } return buffer.toString(); } 刚才那个loadStream方法,如果dir里面有汉字则显示为乱码。应将该方法修改为如下: static String loadStream(InputStream in) throws IOException { int ptr = 0; in = new BufferedInputStream(in); StringBuffer buffer = new StringBuffer(); while ( (ptr = in.read()) != -1) { buffer.append( (char) ptr); } String aa=buffer.toString(); byte[] temp= aa.getBytes("ISO-8859-1"); String result=new String(temp); return result; } 上面的几段代码都已经实现成功,感谢上面的兄弟,至于may_young(May)所说的执行出错,我是没有遇到,是不是你的系统有问题啊,你到别人的机器上java一下 有来晚了 想说的都被bbmer(冷月) 说了只好帮你 顶了~ java的算法题 请教大侠 swt构造树问题!(100分相送) 【新手提问】为什么我有的类不能窗口居中,有的又能,有的能用windows装饰,有的也不能!!狂晕.高手救命 如何用 JAVA 实现客户端和服务器端的语音通信 ? java读取文本文件格式的问题? 看看这个程序 枚举类型不能写在case后面吗?高手来看下我的简单代码! 100分高手来拿,quik 求助如何用把java生成的文件放到指定目录下 一个java基本问题,请高手指教. 建立一个Session与URL进行绑定 两个问题:如何获得、设置系统变量如java_home,如何获得网卡mac地址
String buffer; InputStream pin = p.getInputStream();
InputStreamReader cin = new InputStreamReader(pin);
BufferedReader in = new BufferedReader(cin); try {
buffer = in.readLine(); while (buffer != null) {
System.out.println(buffer);
buffer = in.readLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
c:\winnt\system32\ntvdm.exe
设置应用程序环境时出错。选择“关闭”终止应用程序。
这个错误跟直接调用Runtime.getRun.exec(cmd)一样。
如果调用“cmd dir”会停止响应
public class CmdTest
{
public static void main(String[] args) throws IOException
{
Process proc = Runtime.getRuntime().exec("notepad.exe");
}
}
许命令扩展,而开关/C 指明后面跟随的字符串是命令,并在执行命令后关闭DOS 窗口,start
命令会开启一个单独的窗口执行所提供的命令。前一个答复中的“notepad.exe”必须式在系统变量path下
System.out.println(loadStream(process.getErrorStream())); //load the stream static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ( (ptr = in.read()) != -1)
{
buffer.append( (char) ptr);
}
return buffer.toString();
}
static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ( (ptr = in.read()) != -1)
{
buffer.append( (char) ptr);
}
String aa=buffer.toString();
byte[] temp= aa.getBytes("ISO-8859-1");
String result=new String(temp);
return result;
}
只好帮你 顶了~