0

我需要使用java程序执行任何文件。就像在jdk中我们有java,javac ......就像那样

URL url = new URL("http://torrentz.eu/announcelist_116568555");    
    url.openConnection();    
    InputStream reader = url.openStream();    
    FileOutputStream writer = new FileOutputStream("t1.txt");    
    byte[] buffer = new byte[153600];    
    int bytesRead = 0;    
    while ((bytesRead = reader.read(buffer)) > 0)    
    {      
       writer.write(buffer, 0, bytesRead);
       buffer = new byte[153600];
    }
    writer.close();
    reader.close();

   String[] cmd = new String[1];    
  cmd[0]="t1.txt";  
  Process p = Runtime.getRuntime().exec("C:\\Documents and Settings\\INTEL\\My        Documents\\NetBeansProjects\\urldemo\\t1.txt");  

p.destroy();  
}  

}  

这是错误列表

Exception in thread "main" java.io.IOException: Cannot run program "C:\Documents": CreateProcess error=2, The system cannot find the file specified  
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)  
    at java.lang.Runtime.exec(Runtime.java:617)  
    at java.lang.Runtime.exec(Runtime.java:450)  
    at java.lang.Runtime.exec(Runtime.java:347)  
    at urldemo.Urldemo.main(Urldemo.java:58)  
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified  
    at java.lang.ProcessImpl.create(Native Method)  
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:376)  
    at java.lang.ProcessImpl.start(ProcessImpl.java:136)  
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)  
4

2 回答 2

2

一旦你解决了路径问题,你会发现你无法在 Windows 上执行文本文件,因为文本文件不是可执行程序。如果要打开文件而不是执行它,请使用 Desktop 类。例如,请参阅如何从 Java 启动给定文件的默认(本机)应用程序?

File file = new File ("c:/documents and settings/Intel/whatever/file.txt");
Desktop.getDesktop().open(file);
于 2013-07-23T06:19:33.307 回答
0
Process p = Runtime.getRuntime().exec("cmd /c start notepad C:\\Documents and Settings\\INTEL\\My        Documents\\NetBeansProjects\\urldemo\\t1.txt"); 

//你也可以提供其他编辑器代替记事本

于 2013-07-23T06:19:43.113 回答