Programmatically check for compilation errors java file -



Programmatically check for compilation errors java file -

i want programmatically check if java class has compilation errors. can please allow me know how can done?

you can utilize javacompiler api compile class , allow know if there compile errors.

contrived illustration (using string input, can read file instead):

string classname = "somepackage.yourclass"; string body = "package somepackage; " + "public class yourclass { " + "} "; compile(classname, body, temp_path); //temp_path: .class file saved

where compile this:

private static void compile(string classname, string body, path path) throws exception { list<javasourcefromstring> sourcecode = arrays.aslist(new javasourcefromstring(classname, body)); javacompiler compiler = toolprovider.getsystemjavacompiler(); standardjavafilemanager filemanager = compiler.getstandardfilemanager(null, null, null); filemanager.setlocation(standardlocation.class_output, arrays.aslist(path.tofile())); boolean ok = compiler.gettask(null, filemanager, null, null, null, sourcecode).call(); system.out.println("compilation ok = " + ok); }

java

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -