Using Runtime # exec () with javac - No class file

The following MyCompilingUtility.java creates a file (Java class - AutoGenerated.java ) through PrintWriter . This class has only the Main method with the print operator. 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 starting javac MyCompilingUtility.java && java MyCompilingUtility , the AutoGenerated.class file is not created.

Why is this?

0
source share
1 answer

You are missing ; in the end

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

The compiler just does not compile it.

+1
source

Source: https://habr.com/ru/post/888487/


All Articles