简体   繁体   English

使用Runtime#exec()编译和执行Java代码

[英]Compiling and executing Java code using Runtime#exec()

import java.io.*;

public class Auto {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        try {
            Runtime.getRuntime().exec("javac C:/HelloWorld.java");
            Runtime.getRuntime().exec("java C:/HelloWorld > C:/out.txt");
            System.out.println("END");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This program is able to compile the 'HelloWorld.java' file, but not execute it(HelloWorld). 该程序能够编译“ HelloWorld.java”文件,但不能执行该文件(HelloWorld)。 Could anyone please suggest me how to make it work? 有人可以建议我如何使它工作吗? Thanks in Advance! 提前致谢! :) Also, if the output could be able to be taken in another text file say 'output.txt'. :)另外,如果输出可以在另一个文本文件中使用,请说“ output.txt”。

When your run the java program, you must be in your project root directory, and run java package.to.ClassWhichContainsMainMethod 运行java程序时,您必须位于项目的根目录中,并运行java package.to.ClassWhichContainsMainMethod

Runtime.getRuntime().exec() will give you a Process which contains an OutputStream and an InpuStream to the executed application. Runtime.getRuntime().exec()将为您提供一个Process ,该Process包含一个已执行应用程序的OutputStreamInpuStream

You can redirect the InputStream content to your log file. 您可以将InputStream内容重定向到日志文件。

In your case I would use this exec : public Process exec(String command, String[] envp, File dir) like this : 在您的情况下,我将使用以下exec: public Process exec(String command, String[] envp, File dir)像这样:

exec("java HelloWorld", null, new File("C:/"));

To copy data from the inputStream to the file (code stolen on this post ) : 要将数据从inputStream复制到文件( 此帖子中被盗的代码):

public runningMethod(){
    Process p = exec("java HelloWorld", null, new File("C:/"));
    pipe(p.getInputStream(), new FileOutputStream("C:/test.txt"));
}

public void pipe(InputStream in, OutputStream out) {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int writtenBytes;
    while((writtenBytes = in.read(buf)) >= 0) {
        out.write(buf, 0, writtenBytes);
    }
}

3 Points. 3分。

  1. The JavaCompiler was introduced in Java 1.6 to allow direct compilation of Java source from within Java code. JavaCompiler是Java 1.6中引入的,它允许从Java代码中直接编译Java源代码。
  2. ProcessBuilder (1.5+) is an easier/more robust way to launch a Process. ProcessBuilder(1.5+)是一种更轻松/更强大的启动Process的方法。
  3. For dealing with any process, make sure you read and implement all the points of When Runtime.exec() won't . 要处理任何进程,请确保您已阅读并实现了Runtime.exec()不会的所有要点。

You do not execute a ".java" in java. 您不在Java中执行“ .java”。 You execute a class file. 您执行一个类文件。 So change the second line to: 因此,将第二行更改为:

Runtime.getRuntime().exec("cd c:\;java HelloWorld > C:/out.txt");

As for the output, instead of redirecting to a file, you might want to use the inputStream: 对于输出,您可能要使用inputStream而不是重定向到文件:

InputStream is = Runtime.getRuntime().exec("cd c:\;java HelloWorld").getInputStream();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM