简体   繁体   English

使用 ProcessBuilder 编译多个 java 文件抛出 File not found 错误

[英]Using ProcessBuilder to compile multiple java files throws File not found error

I have simple java program for compiling java classes.我有简单的 java 程序来编译 java 类。 I created a JAR of this program and when I run it on Ubuntu I pass to the jar the path of folder with java files. I created a JAR of this program and when I run it on Ubuntu I pass to the jar the path of folder with java files.

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        compile(args[0]);
    }
    //pathToFiles - is a value from command line arguments
    private static void compile(String pathToFiles) throws IOException, InterruptedException {
        List<String> cmdList = new ArrayList<>();
        cmdList.add("javac");
        cmdList.add(pathToFiles); 
        System.out.println("cmd: "+cmdList);


        ProcessBuilder pb = new ProcessBuilder(cmdList);
        Process process = pb.start();
        int exitValue = process.waitFor();
        if (exitValue != 0) {
            generateCompileException(process);
        }


    }
    //method just generates error message if there was an error
    private static void generateCompileException(Process process){
        StringBuilder response = new StringBuilder();
        try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
            String line;
            if ((line = b.readLine()) != null)
                response.append(line);
        } catch (final IOException e) {
            e.printStackTrace();
        }
        throw new RuntimeException(response.toString());
    }
}

When I pass path containing single java file it works:当我传递包含单个 java 文件的路径时,它可以工作:

java -jar co-1.jar /home/admin/test2/Calculator.java

But I want to compile multiple files.但我想编译多个文件。 When I pass path containing multiple files I get error: file not found.当我传递包含多个文件的路径时,出现错误:找不到文件。

java -jar co-1.jar '/home/admin/test2/*.java'

在此处输入图像描述

PS: If I run a javac command manually with multiple files, it will work: PS:如果我使用多个文件手动运行 javac 命令,它将起作用: 在此处输入图像描述

Remove quotes and use the command as below.删除引号并使用如下命令。

java -cp co-1.jar:/home/admin/test2/* Main.class <args>

See also也可以看看

https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

PS: Unix uses:(colon) as delimiter and windows uses;(semi-colon) delimiter to separate multiple paths. PS:Unix 使用:(冒号)作为分隔符,windows 使用;(分号)分隔多个路径。

ProcessBuilder will not evaluate wildcards, as that is a feature of your terminal (such as bash). ProcessBuilder 不会评估通配符,因为这是您的终端(例如 bash)的一项功能。 If you want wildcard to be expanded you need to run bash inside ProcessBuilder command, such as:如果要扩展通配符,则需要在ProcessBuilder命令中运行 bash ,例如:

ProcessBuilder pb = new ProcessBuilder("bash", "-c", commandContainingWildcard);
... // start() etc

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

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