如何运行类java使用runtime.exec()?

时间:2014-03-19 02:39:27

标签: java process runtime.exec

我必须通过另一个程序运行java类java使用runtime.exec()。 文件夹中的一个类与我的程序不同,所以我无法运行它。 我用过(java -cp"目录"类),但是我的类有导入文件所以它不能发送文件? 谁能帮帮我?

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 Files \ 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 \ classes,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:\用户\ QUANG HUY \ Documents \ NetBeansProjects \ ChamDiemJava \ build \ classes,C:\ Users \ QUANG HUY \ Documents \ NetBeansProjects \ ChamDiemJava \ src] [加载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)]] [加载ZipFileIndexFileObject [C:\ Program Files \ Java \ jdk1.7.0 \ lib \ ct.sym(META-INF / sym / rt.jar / java / io / FileNotFoundException.class)]] [加载ZipFileIndexFileObject [C:\ Program 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)]] [加载ZipFileIndexFileObject [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-INF / 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] 建立成功(总时间:2秒)

1 个答案:

答案 0 :(得分: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();
    }

}