package test;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class ReadMAC{
public static String physicalAddress = "read MAC error!";
public ReadMAC() {}
public static String checkPhysicalAddress(){
try{
String line;
Process process = Runtime.getRuntime().exec("cmd /c ipconfig /all");
System.out.println("befor process.waitFor();");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// process.waitFor(); //这句话加在这里执行ipconfig /all命令程序就停在这里了
System.out.println("after process.waitFor();");
while ( (line=bufferedReader.readLine()) != null){
if(line.indexOf("Physical Address. . . . . . . . . :") != -1){
if(line.indexOf(":") != -1){
physicalAddress = line.substring(line.indexOf(":")+2);
break; //找到MAC,推出循环
}
}
}
process.waitFor(); //这句话加在这里执行ipconfig /all命令程序就停正常了
}
catch(IOException ioe)
{
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return physicalAddress;
}
public static void main(String[] args)
{
System.out.println("本机的MAC地址是: "+ ReadMAC.checkPhysicalAddress());
}
}如上面代码中注释中提到的,process.waitFor(); 加在不同的地方结果是不一样的。但是为什么呢?请高手赐教。谢谢。
在这里再问达人一个问题,就是Runtime.getRuntime().exec方法中的参数到底是怎样的?规则等。最好还请高手们能举例说明!再次谢谢个位达人了!谢谢!
import java.io.IOException;
import java.io.InputStreamReader;public class ReadMAC{
public static String physicalAddress = "read MAC error!";
public ReadMAC() {}
public static String checkPhysicalAddress(){
try{
String line;
Process process = Runtime.getRuntime().exec("cmd /c ipconfig /all");
System.out.println("befor process.waitFor();");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// process.waitFor(); //这句话加在这里执行ipconfig /all命令程序就停在这里了
System.out.println("after process.waitFor();");
while ( (line=bufferedReader.readLine()) != null){
if(line.indexOf("Physical Address. . . . . . . . . :") != -1){
if(line.indexOf(":") != -1){
physicalAddress = line.substring(line.indexOf(":")+2);
break; //找到MAC,推出循环
}
}
}
process.waitFor(); //这句话加在这里执行ipconfig /all命令程序就停正常了
}
catch(IOException ioe)
{
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return physicalAddress;
}
public static void main(String[] args)
{
System.out.println("本机的MAC地址是: "+ ReadMAC.checkPhysicalAddress());
}
}如上面代码中注释中提到的,process.waitFor(); 加在不同的地方结果是不一样的。但是为什么呢?请高手赐教。谢谢。
在这里再问达人一个问题,就是Runtime.getRuntime().exec方法中的参数到底是怎样的?规则等。最好还请高手们能举例说明!再次谢谢个位达人了!谢谢!
楼主【wkw_xuexi】截止到2008-07-27 00:48:00的历史汇总数据(不包括此帖):
发帖的总数量:2 发帖的总分数:120 每贴平均分数:60
回帖的总数量:2 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:2 结贴的总分数:120
无满意结贴数:1 无满意结贴分:10
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:50.00 % 无满意结分率:8.33 %
敬礼!
ldr.destroy();
一般放在最后,注意前面的流要关闭。
还有,就是Runtime.getRuntime().exec方法。如果我想执行这样的操作应该怎么用?首先我想用CD命令把路径转到D:\Program Files\MySQL\MySQL Server 5.0\bin,然后在D:\Program Files\MySQL\MySQL Server 5.0\bin中执行mysql的相关命令(如:mysql -uroot -p*****)应该怎么样?谢谢赐教!谢谢!
process.getErrorStream();
mysql -uroot -p*****)
这些应该做为参数加上去,
比如果我要运行TTPLAY /**
* 测试runtime
*
*/
public static void do1(){
try{
Process myproc = Runtime.getRuntime().exec("C:/Program Files/TTPlayer/TTPlayer.exe E:/My_music/miss.mp3");
myproc.getErrorStream();
}
catch(Exception e){}
}