如何在JAVA程序中调用windows其他程序

虽然JAVA是平台无关性的,但是在企业中很多时候还是在为特定的系统在开发,会要求调用一些当前系统的其他程序或命令。最常见的是在WINDOWS中。其实JAVA是可以通过Runtime去调用系统中的一些程序的,下面是一个例子:

try {
            ps 
=  Runtime.getRuntime().exec( " E:\\test.exe " );
            
// ps = Runtime.getRuntime().exec("ipconfig");  ---- For execute windows commands
            
// ps = Runtime.getRuntime().exec("E:\\test.bat"); ---- For run BAT files
            BufferedReader in  =   new  BufferedReader( new    InputStreamReader(ps.getInputStream()));
            String inputLine; 
            
while ((inputLine    =    in.readLine())    !=     null {  
                result   
+=    inputLine + " \n " ;
            }

            in.close();
            System.out.println(
" Output: "   +  result);
            
        }
catch (Exception ex) {
            System.out.println(
" Error "   +  ex.getMessage());
        }

上面的代码片断中后面一部分是在取返回的参数,如果不需要可以不取。不取的话可能也就不需要取得到Process了。用这个方法可以运行windows中的exe或者bat文件。

posted on 2006-12-20 10:49 EdwinWang 阅读(1148) 评论(1)  编辑  收藏 所属分类: JAVA相关

评论

# re: 如何在JAVA程序中调用windows其他程序 2014-07-31 15:08 mdd

Java调用windows刻录光盘  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜