@@ -88,8 +88,9 @@ public static Object construct(String canonicalClassName){
8888 * @param className - full class name including package, e.g. com.example.my_package.MyNewClass
8989 * @param classCode - source-code as seen in Java files
9090 * @param targetFolder - parent directory of compiled class file (without package-path) or null
91+ * @return Compile errors as readable String or empty
9192 */
92- public static void compile (String className , String classCode , File targetFolder ){
93+ public static String compile (String className , String classCode , File targetFolder ){
9394 JavaCompiler compiler = ToolProvider .getSystemJavaCompiler ();
9495 if (compiler == null ){
9596 String msg = "Cannot find Java compiler! "
@@ -122,12 +123,16 @@ public static void compile(String className, String classCode, File targetFolder
122123 if (task .call ()){
123124 //Done
124125 Debugger .println ("ClassBuilder - compiled '" + className + "' to '" + folderOrMemory , 3 );
126+ return "" ;
125127 }else {
126128 //Error(s)
129+ StringBuilder errors = new StringBuilder ("Compile errors: \n " );
127130 for (Diagnostic <? extends JavaFileObject > diagnostic : diagnostics .getDiagnostics ()){
128- Debugger .println ("ClassBuilder - compiling of '" + diagnostic .getSource ().toUri () + "' failed. Error: \n " +
129- "Line " + diagnostic .getLineNumber () + ": " + diagnostic .getMessage (Locale .ENGLISH ), 1 );
131+ String error = diagnostic .getSource ().toUri () + " - " + "Line " + diagnostic .getLineNumber () + ": " + diagnostic .getMessage (Locale .ENGLISH );
132+ errors .append (error ).append (" \n " );
133+ Debugger .println ("ClassBuilder - compiling of '" + error , 1 );
130134 }
135+ return errors .toString ();
131136 }
132137 }
133138
0 commit comments