public class CallExe{     public static void main(String args[]){         Runtime rn=Runtime.getRuntime();         Process p=null; 
        try{             p=rn.exec("cmd.exe /c helloWorld.exe");         }catch(Exception e){             System.out.println("Error exec helloWorld!");         }     } } 看网上的资料,提供了这种调用exe文件的方法。但我试过,exe文件没有执行,然后,我又试着把helloWorld.exe换成 notepad 或者 某个txt文件,能正常执行。请各位指教指教,怎样才能正常执行exe文件。

解决方案 »

  1.   

    你这个helloWorld.exe确实存在么?是在windwos环境变量path能找到的地方么?
      

  2.   

    把path值多设一个  增加一个到你的那个exe文件的位置
      

  3.   

    import java.io.IOException;public class Ping{    /**
         * @param args
         * String的split方法也是可以分割字符串的
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {        
                java.lang.Runtime.getRuntime().exec("cmd.exe /c start D:\\test.exe");//这儿
                process.waitFor();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    }
      

  4.   

    因为你没有指出helloWorld.exe文件的路径
    楼上正解
      

  5.   

    --相对路径   
    package test; 
    class TestFrame
    ...
    Process child = Runtime.getRuntime().exec(TestFrame.class.getResource("/test/abc.exe").getPath());
    ...
    运行或打包时将abc.exe和TestFrame.class放同一目录--绝对路径
    Process child = Runtime.getRuntime().exec("E:\\abc.exe"); 
      

  6.   

    .exe打到jar包后不能运行,不知道打包后该用什么方式读取出来让其运行 ~
      

  7.   

    import java.io.IOException;public class Ping{    /**
         * @param args
         * String的split方法也是可以分割字符串的
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {        
                Process p=java.lang.Runtime.getRuntime().exec("cmd.exe /c start E:\\_WorkBench\\HelloWorld\\HelloWorld.exe");//注意这里
                p.waitFor();//注意这里
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    }
    上面的代码,我在java6.0下运行成功。而3楼hmsuccess的代码在我这里编译错误,我只是修改了try块中的两行代码。
      

  8.   

    java.lang.Runtime.getRuntime().exec("cmd.exe /c start D:\\test.exe");//这儿
      

  9.   

    public class Demo{ 
    public static void main(String args[]){ 
    Runtime rn=Runtime.getRuntime(); 
    Process p=null; try{ 
    p=rn.exec(""D:/AnyQ/AnyQ.exe""); 
    }catch(Exception e){ 
    System.out.println("Error exec AnyQ"); 


    }