求教Java创建外部进程?(初学) 小弟初学Java求教Java如何执行外部程序,如象exec或CreatProcess()之类的函数,要是在Linux下如何做呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该差不多吧只要你熟悉linux的命令 呵呵,Win下用Java实现偶也不知道啊。 try{ String[] command=new String[]{"D:\\Office\\Microsoft Office\\Office10\\WINWORD.EXE","E:\\tt.doc"}; Process prop=Runtime.getRuntime().exec(command); }catch(Exception ex) { ex.printStackTrace(); } 给你一个jdk5.0下的例子:import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;public class ProcessBuilderDemo { public static void main(String[] args) { //Physical Address. . . . . . . . . : InputStream input = null; try{ //执行命令 ProcessBuilder builder = new ProcessBuilder("ipconfig" ,"/all"); Process process = builder.start(); input = process.getInputStream(); //把得到的流得到 byte[] b = new byte[1024]; StringBuffer buffer = new StringBuffer(); while (input.read(b) > 0) { buffer.append(new String(b)); } //分析流 String value = buffer.substring(0); String systemFlag = "Physical Address. . . . . . . . . :"; int index = value.indexOf(systemFlag); List<String> address = new ArrayList<String>(); if (0 < index) { value = buffer.substring(index + systemFlag.length()); address.add(value.substring(0, 18)); } //打印输出 for (String add : address) { System.out.println(add); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { input.close(); } catch (IOException ex) { ex.printStackTrace(); } } }} jdk1.4下没有ProcessBuilder类 不过可以通过Runtime的exec方法执行 基本原理一样 呵呵 这个例子是获得本机的MAC地址 恩,win下的我试过了,谢谢norikos(zhulgxg)啊 。可是Linux下是不是一样呢?不如要执行RedHat自带的计算器如何实现啊。 在Linux下,或者是有个pro1的程序,我试Process prop=Runtime.getRuntime().exec("pro1");是不能运行的。求助啊。 linux:try { String command = "/bin/echo"; command += " 'FirstLine\nSecond Line'"; command +=" | /usr/bin/mail"; command +=" -a 'From: someone'"; command +=" -s 'Subject'"; command +=" [email protected]"; // /bin/sh是unix下的cmd Process send = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",command });} catch (IOException e) { System.out.println(e.toString());}//FOR WINDOWS 95 AND 98 USE COMMAND.COMif(osName.equals("Windows 95") || osName.equals("Windows 98")){ Runtime.getRuntime().exec("command.com /C start acrord32 /p /h" + claim.pdf);}//FOR WINDOWS NT/XP/2000 USE CMD.EXEelse { Runtime.getRuntime().exec("cmd.exe start /C acrord32 /p /h" + claim.pdf);} JMX问题求助 JTable 的使用 分油问题 jdk1.5,遇到类库不存在问题 刚刚学jsp 请指教了,谢谢 走J2EE路线需要看那些书 DefaultRenderingErrorListener.errorOccurred: 关于String的split方法 repaint的问题 JDO和Entity Bean有什么区别? jvm字节码指令是原子的吗? 大家帮忙看看这几道题选什么,谢谢! HIDE_ON_CLOSE和DISPOSE_ON_CLOSE的区别
只要你熟悉linux的命令
String[] command=new String[]{"D:\\Office\\Microsoft Office\\Office10\\WINWORD.EXE","E:\\tt.doc"};
Process prop=Runtime.getRuntime().exec(command);
}catch(Exception ex)
{
ex.printStackTrace();
}
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;public class ProcessBuilderDemo {
public static void main(String[] args) {
//Physical Address. . . . . . . . . :
InputStream input = null;
try{
//执行命令
ProcessBuilder builder = new ProcessBuilder("ipconfig" ,"/all");
Process process = builder.start();
input = process.getInputStream();
//把得到的流得到
byte[] b = new byte[1024];
StringBuffer buffer = new StringBuffer();
while (input.read(b) > 0) {
buffer.append(new String(b));
}
//分析流
String value = buffer.substring(0);
String systemFlag = "Physical Address. . . . . . . . . :";
int index = value.indexOf(systemFlag);
List<String> address = new ArrayList<String>();
if (0 < index) {
value = buffer.substring(index + systemFlag.length());
address.add(value.substring(0, 18));
} //打印输出
for (String add : address) {
System.out.println(add);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
input.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
是不能运行的。求助啊。
try {
String command = "/bin/echo";
command += " 'FirstLine\nSecond Line'";
command +=" | /usr/bin/mail";
command +=" -a 'From: someone'";
command +=" -s 'Subject'";
command +=" [email protected]";
// /bin/sh是unix下的cmd
Process send = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",command });
} catch (IOException e) {
System.out.println(e.toString());
}
//FOR WINDOWS 95 AND 98 USE COMMAND.COM
if(osName.equals("Windows 95") || osName.equals("Windows 98")){
Runtime.getRuntime().exec("command.com /C start acrord32 /p /h" + claim.pdf);
}
//FOR WINDOWS NT/XP/2000 USE CMD.EXE
else {
Runtime.getRuntime().exec("cmd.exe start /C acrord32 /p /h" + claim.pdf);
}