Home > database >  Using ProcessBuilder to compile multiple java files throws File not found error
Using ProcessBuilder to compile multiple java files throws File not found error

Time:09-03

I have simple java program for compiling java classes. 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 -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'

enter image description here

PS: If I run a javac command manually with multiple files, it will work: enter image description here

###################################

UPDATE:

I've added bash command to ProcessBuilder:

private static void compile(String pathToFiles) throws IOException, InterruptedException {
    List<String> cmdList = new ArrayList<>();
    cmdList.add("bash");
    cmdList.add("-c");
    cmdList.add("javac");
    cmdList.add(pathToFiles);
    System.out.println("Processor builder command: " cmdList);
    ProcessBuilder pb = new ProcessBuilder(cmdList);
    Process process = pb.start();
    int exitValue = process.waitFor();
    if (exitValue != 0) {
        System.out.println("Finished with error. Exit value: " exitValue);
        generateCompileException(process);
    }
}

But process withished with error code 2 with empty response from ProcessBuilder.

PS: RuntimeException was thrown by this line: throw new RuntimeException(response.toString()); enter image description here

CodePudding user response:

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

PS: Unix uses :(colon) as delimiter and windows uses ;(semi-colon) delimiter to separate multiple paths.

CodePudding user response:

ProcessBuilder will not evaluate wildcards, as that is a feature of your terminal (such as bash). If you want wildcard to be expanded you need to run bash inside ProcessBuilder command, such as:

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