0

The following MyCompilingUtility.java creates a file (a Java class - AutoGenerated.java) via a PrintWriter. This class only has a Main method with a print statement. Secondly, this program calls javac AutoGenerated.java.

public static void main(String args[]) throws IOException, 
   FileNotFoundException, UnsupportedEncodingException {
    createFile("AutoGenerated.java");
    compile("AutoGenerated.java");
}

public static void compile(String fileName) throws IOException {
    final String javacPath = "C:/Program Files/Java/jdk1.7.0_51/bin/javac.exe";
    Runtime rt = Runtime.getRuntime();
    final String compileCmd = javacPath + " " + fileName;
    rt.exec(compileCmd);
}

public static void createFile(String fileName) throws FileNotFoundException,
       UnsupportedEncodingException  {
    PrintWriter writer = new PrintWriter(fileName, "UTF-8");
    writer.println("public class AutoGenerated");
    writer.println("{");
    writer.println("  public static void main(String [] args)");
    writer.println("  {");
    writer.println("    System.out.println(\"Hello from AutoGenerated World\")");
    writer.println("  }");
    writer.println("}");
    writer.close();
}

However, after running javac MyCompilingUtility.java && java MyCompilingUtility, there's no AutoGenerated.class file produced.

Why is that?

2
  • Get the Process from exec() and call its waitFor method. What code did it return? Commented Feb 5, 2014 at 15:13
  • I should've read the docs. @SotiriosDelimanolis. Running waitFor returns 1. These docs(docs.oracle.com/javase/7/docs/api/java/lang/…) say 0 indicates normal completion. Commented Feb 5, 2014 at 15:19

1 Answer 1

1

You're missing a ; at the end of

writer.println("    System.out.println(\"Hello from AutoGenerated World\");");
                                                                          ^

The compiler simply fails to compile it.

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

1 Comment

@KevinMeredith For future reference, steps to reproduce: ran your code, modified it to add check for the exit code, read the error InputStream (Process#getErrorStream()) and it said exactly the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.