编译运行一个java程序_如何从另一个Java程序编译和运行Java程序

编译运行一个java程序

Have you ever thought if it’s possible to compile and run a java program from another java program? We can use Runtime.exec(String cmd) to issue commands to the underlying operating system. We will use the same approach to compile and run a java program from another java program.

您是否曾经想过是否可以从另一个Java程序编译并运行一个Java程序? 我们可以使用Runtime.exec(String cmd)向底层操作系统发出命令。 我们将使用相同的方法从另一个Java程序编译并运行一个Java程序。

从另一个Java程序编译并运行Java程序 (Compile and Run Java Program from another Java Program)

Let’s write a simple java program that will be compiled and run from another java program.

让我们编写一个简单的Java程序,该程序将从另一个Java程序编译并运行。

package com.journaldev.files;

public class Test {

    public static void main(String[] args) {
        System.out.println("Start");
        for(String str : args){
            System.out.println(str);
        }

    }

}

Here is the other program where I am compiling and running the Test class.

这是我正在编译和运行Test类的另一个程序。

package com.journaldev.files;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class CompileRunJavaProgram {

    public static void main(String[] args) {
        try {
            runProcess("pwd");
            System.out.println("**********");
            runProcess("javac -cp src src/com/journaldev/files/Test.java");
            System.out.println("**********");
            runProcess("java -cp src com/journaldev/files/Test Hi Pankaj");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    private static void printLines(String cmd, InputStream ins) throws Exception {
        String line = null;
        BufferedReader in = new BufferedReader(
            new InputStreamReader(ins));
        while ((line = in.readLine()) != null) {
            System.out.println(cmd + " " + line);
        }
      }

      private static void runProcess(String command) throws Exception {
        Process pro = Runtime.getRuntime().exec(command);
        printLines(command + " stdout:", pro.getInputStream());
        printLines(command + " stderr:", pro.getErrorStream());
        pro.waitFor();
        System.out.println(command + " exitValue() " + pro.exitValue());
      }

}

Notice the difference in javac and java commands. We need to do this because Eclipse working directory is the project root directory but my classes source directory is src.

注意javacjava命令的区别。 我们需要这样做,因为Eclipse工作目录是项目的根目录,但是我的类源目录是src。

When I run the above program from Eclipse, here is the output produced.

当我从Eclipse运行上述程序时,这是产生的输出。

pwd stdout: /Users/pankaj/Documents/eclipse-workspace/JavaExceptions
pwd exitValue() 0
**********
Path Serapartor = /
javac -cp src src/com/journaldev/files/Test.java exitValue() 0
**********
java -cp src com/journaldev/files/Test Hi Pankaj stdout: Start
java -cp src com/journaldev/files/Test Hi Pankaj stdout: Hi
java -cp src com/journaldev/files/Test Hi Pankaj stdout: Pankaj
java -cp src com/journaldev/files/Test Hi Pankaj exitValue() 0

Here is the output when I run the same program from the command line, the working directory is the project root directory.

这是当我从命令行运行相同程序时的输出,工作目录是项目根目录。

pankaj:~ pankaj$ cd /Users/pankaj/Documents/eclipse-workspace/JavaExceptions
pankaj:JavaExceptions pankaj$ javac -cp src src/com/journaldev/files/Test.java
pankaj:JavaExceptions pankaj$ javac -cp src src/com/journaldev/files/CompileRunJavaProgram.java 
pankaj:JavaExceptions pankaj$ java -cp src com/journaldev/files/CompileRunJavaProgram
pwd stdout: /Users/pankaj/Documents/eclipse-workspace/JavaExceptions
pwd exitValue() 0
**********
javac -cp src src/com/journaldev/files/Test.java exitValue() 0
**********
java -cp src com/journaldev/files/Test Hi Pankaj stdout: Start
java -cp src com/journaldev/files/Test Hi Pankaj stdout: Hi
java -cp src com/journaldev/files/Test Hi Pankaj stdout: Pankaj
java -cp src com/journaldev/files/Test Hi Pankaj exitValue() 0
pankaj:JavaExceptions pankaj$

The above program will work fine in Unix systems but it will not work in Windows systems because Windows File separator is different from Unix file separators.

上面的程序在Unix系统上可以正常运行,但是在Windows系统中不能运行,因为Windows文件分隔符与Unix文件分隔符不同。

To make sure it’s platform independent, we can pass commands as an argument to the main function. The main function will look like this:

为了确保它与平台无关,我们可以将命令作为参数传递给main函数 。 主要功能如下所示:

public static void main(String[] args) {
    try {
        if(args.length < 2) throw new Exception("Mandatory Arguments missing");
        runProcess(args[0]);
        runProcess(args[1]);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
}

We can also use File.separator to create the commands in platform independent way. We can also get this property from System getProperty method System.getProperty("file.separator").

我们还可以使用File.separator以平台无关的方式创建命令。 我们还可以从System getProperty方法System.getProperty("file.separator")获得此属性。

Above program can be changed like below for system independent code.

上面的程序可以像下面这样更改,用于系统独立代码。

String separator = File.separator;
System.out.println("File Serapartor = "+separator);

separator = System.getProperty("file.separator");
System.out.println("File Serapartor = "+separator);

runProcess("javac -cp src src"+separator+"com"+separator+"journaldev"+separator+"files"+separator+"Test.java");
System.out.println("**********");
runProcess("java -cp src com"+separator+"journaldev"+separator+"files"+separator+"Test Hi Pankaj");

You will get the same output as above. That’s all for using Runtime exec method to compile and run a java program from another java program.

您将获得与上述相同的输出。 这就是使用Runtime exec方法从另一个Java程序编译并运行一个Java程序的全部内容。

The printLines() and runProcess() methods are taken from this post.

printLines()和runProcess()方法摘自这篇文章

翻译自: https://www.journaldev.com/937/compile-run-java-program-another-java-program

编译运行一个java程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值