1 28 package net.sf.jasperreports.engine.design; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.File ; 32 import java.io.OutputStream ; 33 import java.lang.reflect.Constructor ; 34 import java.lang.reflect.Method ; 35 36 import net.sf.jasperreports.engine.JRException; 37 import net.sf.jasperreports.engine.util.JRClassLoader; 38 39 40 44 public class JRJdk12Compiler extends JRAbstractMultiClassCompiler 45 { 46 47 48 51 public String compileClasses(File [] sourceFiles, String classpath) throws JRException 52 { 53 String [] source = new String [sourceFiles.length + 2]; 54 for (int i = 0; i < sourceFiles.length; i++) 55 { 56 source[i] = sourceFiles[i].getPath(); 57 } 58 source[sourceFiles.length] = "-classpath"; 59 source[sourceFiles.length + 1] = classpath; 60 61 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 62 63 try 64 { 65 Class javacClass = JRClassLoader.loadClassForName("sun.tools.javac.Main"); 66 Constructor constructor = javacClass.getConstructor(new Class [] {OutputStream .class, String .class}); 67 Method compileMethod = javacClass.getMethod("compile", new Class [] {String [].class}); 68 Object javac = constructor.newInstance(new Object [] {baos, source[0]}); 69 70 compileMethod.invoke(javac, new Object [] {source}); 71 } 72 catch (Exception e) 73 { 74 StringBuffer files = new StringBuffer (); 75 for (int i = 0; i < sourceFiles.length; ++i) 76 { 77 files.append(sourceFiles[i].getPath()); 78 files.append(' '); 79 } 80 throw new JRException("Error compiling report java source files : " + files, e); 81 } 82 83 if( baos.toString().indexOf("error") != -1 ) 84 { 85 return baos.toString(); 86 } 87 88 return null; 89 } 90 91 92 } 93 | Popular Tags |