2014-03-19 120 views
0

我必須通過另一個程序java運行一個java類使用runtime.exec()。 文件夾中的類與我的程序不同,所以我無法運行它。 我用(java -cp「目錄」類),但我的課有導入文件,所以它不能filenotfound? 誰能幫幫我?如何運行class java使用runtime.exec()?

Process pro = Runtime.getRuntime()。exec(「java -cp \」E:\ DA_Laptrinhmang \「NguyenTo」);

下面是程序:

import java.io.*; 
import javax.tools.JavaCompiler; 
import javax.tools.ToolProvider; 
public class TestCodeJava { 
    private static void printLines(String name, InputStream ins) throws Exception { 
    String line = null; 
    BufferedReader in = new BufferedReader(new InputStreamReader(ins)); 
    while ((line = in.readLine()) != null) { 
     System.out.println(name + " " + line); 
    } 
    } 
    private static void runProcess(String command) throws Exception { 
    Process pro = Runtime.getRuntime().exec(command); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(pro.getInputStream())); 
    pro.waitFor(); 
    String line = ""; 
    while((line = reader.readLine())!=null) 
    { 
     System.out.println(line); 
    } 
    } 
    private static int toCompile(String fileToCompile) 
    { 
     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 
     int compilationResult = compiler.run(null, null, null, "-verbose", fileToCompile); 
     return compilationResult; 
    } 
    public static void main(String[] args) { 
    try { 
     if(toCompile("E:\\Folder\\Prime.java")==0) 
     { 
     runProcess("java -cp \"E:\\Folder\" Prime"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

    import java.io.*; 
    public class Prime { 
    private static boolean isPrime(int n) 
    { 
     if(n <= 1) return false; 
     if(n == 2 || n == 3) return true; 
     int m = (int) Math.sqrt(n); 
     for(int i = 5; i <= m; i = i + 5) 
      if(n % i == 0 || n % (i+2) == 0) return false; 
     return true; 
    } 
    public static void main(String args[]) throws FileNotFoundException, IOException 
    { 
     File file = new File("INPUT.TXT"); 
     FileInputStream fis = new FileInputStream(file); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 
     int n = Integer.parseInt(reader.readLine()); 
     File file2 = new File("OUTPUT.TXT"); 
     FileOutputStream fos = new FileOutputStream(file2); 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos)); 
     if(isPrime(n)) 
     { 
      writer.write("YES"); 
     }else writer.write("NO"); 
     writer.flush(); 
     writer.close(); 
    } 
} 

這裏錯誤: [解析開始RegularFileObject [E:\ DA_Laptrinhmang \ NguyenTo.java] [解析完成42ms] [源文件搜索路徑:C :\ Users \ QUANG HUY \ Documents \ NetBeansProjects \ ChamDiemJava \ build \ classes,C:\ Users \ QUANG HUY \ Documents \ NetBeansProjects \ ChamDiemJava \ src] [類文件搜索路徑:C:\ Program Files \ Java \ jdk1 .7.0 \ jre \ lib \ resources.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ rt.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ sunrsasign.jar ,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ jsse.jar,C:\ Program F iles \ Java \ jdk1.7.0 \ jre \ lib \ jce.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ charsets.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \類,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ ext \ dnsns.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ ext \ localedata.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ ext \ sunec.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ ext \ sunjce_provider.jar,C:\ Program Files \ Java \ jdk1。 7.0 \ jre \ lib \ ext \ sunmscapi.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ ext \ sunpkcs11.jar,C:\ Program Files \ Java \ jdk1.7.0 \ jre \ lib \ ext \ zipfs.jar,C:\ Users \ QUANG HUY \ Documents \ NetBeansProjects \ ChamDiemJava \ build \ classes,C:\ Users \ QUANG HUY \ Documents \ NetBeansProjects \ ChamDiemJava \ src] [loading ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]] [lo在C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/FileNotFoundException.class)]]中加載ZipFileIndexFileObject [加載ZipFileIndexFileObject [C:\ Program Files Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/IOException.class)]] [正在檢查NguyenTo] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/Math.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META- INF/sym/rt.jar/java/lang/Exception.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar /java/lang/Throwable.class)]] [loading Zip FileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/File.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/net/URI.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/FileInputStream.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META -INF/sym/rt.jar/java/io/FileDescriptor.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt。 jar/java/io/BufferedReader.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/InputStreamReader .class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-I NF/sym/rt.jar/java/io/InputStream.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar /java/io/Reader.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt。jar/java/lang/Integer.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/Number .class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/FileOutputStream.class)]] [加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/BufferedWriter.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/OutputStreamWriter.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/io/OutputStream.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym( META-INF/sym/rt.jar/java/io/Writer.class)]] [加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/Error.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/RuntimeException.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1。 7.0 \ lib \ ct.sym(META-INF/sym/rt.jar/java/lang/NumberFormatException.class)]] [正在加載ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym (META-INF/sym/rt.jar/java/lang/IllegalArgumentException.class)]] [寫出RegularFileObject [E:\ DA_Laptrinhmang \ NguyenTo.class]] [共441ms] BUILD SUCCESSFUL(總時間:2秒)

+1

而你得到什麼錯誤?爲什麼它不工作?你是否試過從應用程序所在的上下文中的命令行運行它? – MadProgrammer

+0

我嘗試通過cmd在E盤上運行代碼java,但是我不能,因爲我的類有導入文件。我需要使用runtime.exect()通過另一個程序來執行代碼java。 – Student

+0

我看到似乎是類路徑(不正確轉義)的一部分,但沒有主類或jar文件。 –

回答

1

這對我有用

package mypackage; 

import java.io.IOException; 

public class RunSomeJava { 

    public static void main(String[] args) throws IOException { 
     Process pro = Runtime.getRuntime().exec("java -cp E:\\workspaceIndigo2\\StackOverflow\\bin mypackage.SomeOtherProgram"); 
    } 

} 

package mypackage; 

import java.io.File; 
import java.io.IOException; 

public class SomeOtherProgram { 

    public static void main(String[] args) throws IOException { 
     File f = new File(System.currentTimeMillis()+".txt"); 
     f.createNewFile(); 
    } 

} 
+0

但是,如果我從文件讀取數據然後錯誤filenotfound – Student

+0

你修改了問題,這是另一個問題 – Leo

+0

這不是很好,先生@學生 – Leo

相關問題