- //File path eg. fileName = "c://compilerTest.java"
- String fileName = " "
- // get Java Compiler
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- // get FileManager to manage the file that you want to compile.
- StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null);
- //get file object representing the given file names.
- Iterable units = fileMgr.getJavaFileObjects(fileName);
- // get compilation task
- CompilationTask t = compiler.getTask(null, fileMgr, null, null, null, units);
- // Performs this compilation task. then you can get the Class of the given fileName.
- t.call();
- fileMgr.close();
复制代码 |
|