2

Im trying to compile some java files using the Runtime.getRuntime.exec(command) and im generating the command. The code below shows what i am doing:

String command = "javac ";
    for(String s: this.getPackages())
    {
        command = command + "/home/benuni/CompileFiles/" + project + "/" + s + "/*.java ";
    }

    try {
        System.out.println("command: '"+ command +"'");
        Process pro = Runtime.getRuntime().exec(command);
         printLines(" stderr:", pro.getErrorStream());

        pro.waitFor();

This is giving the following output:

command: 'javac /home/benuni/CompileFiles/HelloWorldProject/HelloWorldPackage/*.java '
stderr: javac: file not found:     /home/benuni/CompileFiles/HelloWorldProject/HelloWorldPackage/*.java
 stderr: Usage: javac <options> <source files>
 stderr: use -help for a list of possible options

and its not working.. but if i copy the command into my shell, it works no problem... any ideas?

1
  • Try replacing *.java with any one file that is in /home/benuni/CompileFiles/HelloWorldProject/HelloWorldPackage/ Commented Aug 23, 2012 at 21:51

3 Answers 3

4

Java doesn't expand the * wildcard for you. That's a facility of the shell. Instead, you'll need to list the directory to get all of the contained files.

Something like this would do the trick to build up your command:

    File[] files = new File("/home/benuni/CompileFiles/" + project + "/" + s).listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".java");
        }
    });

    StringBuilder command = new StringBuilder("javac");
    for(File file : files) {
        command.append(" ").append(file.getAbsolutePath());
    }
Sign up to request clarification or add additional context in comments.

Comments

2

When you enter from the command line, the command analyzer expands * into a list of files. When you use Runtime.exec that expansion does not occur, and you must explicitly specify the entire list.

Comments

1

One option is to provide the actual list of file names to javac. Another is to let shell do it, using the command

/bin/sh -c javac /home/benuni/CompileFiles/HelloWorldProject/HelloWorldPackage/*.java 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.