2

I am trying to use Java's

Process p = Runtime.getRuntime().exec(command); 

to compile .java files in other folder, but it does not work.
My Main.class is in folder a, and all .java files are in folder a/test.

Main.class is :

public class Main{
    public static void main( String[] args )
                throws IOException,InterruptedException{
        String line ="";
        String command = "javac test/*.java";
        Process pro = Runtime.getRuntime().exec(command);
        BufferedReader in = new BufferedReader(
                new InputStreamReader( pro.getInputStream()));
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        BufferedReader er = new BufferedReader(
                new InputStreamReader( pro.getErrorStream()));
        while ((line = er.readLine()) != null) {
            System.out.println(line);
        }
    }
}

And the error stream shows:

javac: file not found: test/*.java

Why does this happen? There are some java file in folder test

1 Answer 1

1
  1. in this case Java doesn't work with * as wildcard, as well as your shell. That means that you would transform * -> list of file_names manually and do exec() for every particular file.

  2. Also be aware on classpath. if you run this class form IDE, classpath could be different from you expect (same folder in which class is placed).

Sign up to request clarification or add additional context in comments.

4 Comments

but why it works if the Main class is in the same folder as other java file? like javac *.java
@Jianson: Actually it is working just fine. Have you added javac to the PATH variable?
@nIcEcOw : Yes, I added it. After I run Main.class, there is no class file in test folder....
@Jianson: On my side, your code is working just fine (.class files are generated inside the said folder ), though only if I use somethingy like String command = "javac -d . ../test/SomeClass.java"; instead of asterisk.

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.