Java Code Examples for com.sun.tools.javac.Main#compile()
The following examples show how to use
com.sun.tools.javac.Main#compile() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: compileTestcase.java From muJava with Apache License 2.0 | 6 votes |
public static void main(String[] args) { Debug.setDebugLevel(3); File f = new File(MutationSystem.TESTSET_PATH); String[] s = f.list(new ExtensionFilter("java")); String[] pars = new String[2+s.length]; pars[0] = "-classpath"; pars[1] = MutationSystem.CLASS_PATH; for (int i=0; i<s.length; i++) { pars[i+2] = MutationSystem.TESTSET_PATH + "/" + s[i]; } try { // result = 0 : SUCCESS, result = 1 : FALSE int result = Main.compile(pars,new PrintWriter(System.out)); if (result == 0) { Debug.println("Compile Finished"); } } catch (Exception e) { e.printStackTrace(); } }
Example 2
Source File: plugin_loading.java From ldparteditor with MIT License | 6 votes |
public void pluginTest() { // List all java files from the plugin directory File F = new File("plugin"); String[] inhalt = F.list(); for (int i = 0; i < inhalt.length; i++) { if (inhalt[i].toLowerCase().endsWith(".java")) { System.out.println(inhalt[i]); } } // Compile the plugin and call a method Main.compile(new String[] { "plugin/LpePlugin.java" }); try { Plugin p = (Plugin) Class.forName("LpePlugin").newInstance(); System.out.println(p.getPlugInName()); } catch (Exception e) { e.printStackTrace(); } }
Example 3
Source File: T8076104.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void doTest(File testJar, String... additionalArgs) { List<String> options = new ArrayList<>(); options.add("-proc:only"); options.add("-processor"); options.add("T8076104"); options.add("-classpath"); options.add(System.getProperty("test.classes") + File.pathSeparator + testJar.getAbsolutePath()); options.addAll(Arrays.asList(additionalArgs)); options.add(System.getProperty("test.src") + File.separator + "T8076104.java"); int res = Main.compile(options.toArray(new String[0])); if (res != 0) { throw new AssertionError("Unexpected error code: " + res); } }
Example 4
Source File: T6558476.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 5
Source File: SunJavaCompiler.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public WorkResult execute(JavaCompileSpec spec) { LOGGER.info("Compiling with Sun Java compiler API."); String[] options = createCommandLineOptions(spec); int exitCode = Main.compile(options); if (exitCode != 0) { throw new CompilationFailedException(); } return new SimpleWorkResult(true); }
Example 6
Source File: T6558476.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 7
Source File: T6558476.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 8
Source File: T6558476.java From hottub with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 9
Source File: BadClass.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static String makeClass(String dir, String filename, String body) throws IOException { File file = new File(dir, filename); try (FileWriter fw = new FileWriter(file)) { fw.write(body); } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); String args[] = { "-cp", dir, "-d", dir, "-XDrawDiagnostics", file.getPath() }; Main.compile(args, pw); pw.close(); return sw.toString(); }
Example 10
Source File: T6558476.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 11
Source File: T6558476.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 12
Source File: T6558476.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { File javaHomeDir = new File(System.getProperty("java.home")); File outputDir = new File("outputDir" + new Random().nextInt(65536)); outputDir.mkdir(); outputDir.deleteOnExit(); File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar"); File tmpJar = copyFileTo(dnsjarfile, outputDir); String className = "TheJavaFile"; File javaFile = new File(outputDir, className + ".java"); javaFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(javaFile); fos.write(generateJavaClass(className).getBytes()); fos.close(); int rc = Main.compile(new String[]{"-d", outputDir.getPath(), "-classpath", tmpJar.getPath(), javaFile.getAbsolutePath()}); if (rc != 0) { throw new Error("Couldn't compile the file (exit code=" + rc + ")"); } if (tmpJar.delete()) { System.out.println("jar file successfully deleted"); } else { throw new Error("Error deleting file \"" + tmpJar.getPath() + "\""); } }
Example 13
Source File: SunJavaCompiler.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
public WorkResult execute(JavaCompileSpec spec) { LOGGER.info("Compiling with Sun Java compiler API."); String[] options = createCommandLineOptions(spec); int exitCode = Main.compile(options); if (exitCode != 0) { throw new CompilationFailedException(); } return new SimpleWorkResult(true); }
Example 14
Source File: QueryBeforeEnter.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test public void testTooSoon(Path base) throws Exception { Path src = base.resolve("src"); tb.writeJavaFiles(src, "package test; public class Test {}"); Path out = base.resolve("out"); Files.createDirectories(out); Path reg = base.resolve("reg"); Path regFile = reg.resolve("META-INF").resolve("services").resolve(Plugin.class.getName()); Files.createDirectories(regFile.getParent()); try (OutputStream regOut = Files.newOutputStream(regFile)) { regOut.write(PluginImpl.class.getName().getBytes()); } String processorPath = System.getProperty("test.class.path") + File.pathSeparator + reg.toString(); JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); Path testSource = src.resolve("test").resolve("Test.java"); try (StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) { com.sun.source.util.JavacTask task = (com.sun.source.util.JavacTask) javaCompiler.getTask(null, null, d -> { throw new IllegalStateException(d.toString()); }, Arrays.asList("--processor-path", processorPath, "-processor", AP.class.getName(), "-Xplugin:test"), null, fm.getJavaFileObjects(testSource)); task.call(); } Main.compile(new String[] {"--processor-path", processorPath, "-Xplugin:test", testSource.toString()}); }
Example 15
Source File: CompileEvent.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
void run() throws IOException { String testClasses = System.getProperty("test.classes"); File pluginRegistration = new File(testClasses + "/META-INF/services/com.sun.source.util.Plugin"); pluginRegistration.getParentFile().mkdirs(); try (Writer metaInfRegistration = new FileWriter(pluginRegistration)) { metaInfRegistration.write("CompileEvent$PluginImpl"); } File test = new File(testClasses + "/Test.java"); test.getParentFile().mkdirs(); try (Writer testFileWriter = new FileWriter(test)) { testFileWriter.write("public class Test { }"); } StringWriter out; //test events fired to listeners registered from plugins //when starting compiler using Main.compile out = new StringWriter(); int mainResult = Main.compile(new String[] { "-XDaccessInternalAPI", "-Xplugin:compile-event", "-processorpath", testClasses, test.getAbsolutePath() }, new PrintWriter(out, true)); if (mainResult != 0) throw new AssertionError("Compilation failed unexpectedly, exit code: " + mainResult); assertOutput(out.toString()); JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { Iterable<? extends JavaFileObject> testFileObjects = fm.getJavaFileObjects(test); //test events fired to listeners registered from plugins //when starting compiler using JavaCompiler.getTask(...).call List<String> options = Arrays.asList("-XDaccessInternalAPI", "-Xplugin:compile-event", "-processorpath", testClasses); out = new StringWriter(); boolean compResult = comp.getTask(out, null, null, options, null, testFileObjects).call(); if (!compResult) throw new AssertionError("Compilation failed unexpectedly."); assertOutput(out.toString()); } }
Example 16
Source File: javac.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
public static int compile(String[] args, PrintWriter printWriter) { return Main.compile(args, printWriter); }
Example 17
Source File: javac.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
public static int compile(String[] args) { return Main.compile(args); }
Example 18
Source File: javac.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
public static void main(String[] zArgs) { int compile = Main.compile(zArgs); }
Example 19
Source File: Javac.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
public static int compile(String[] args, DiagnosticListener listener) { return Main.compile(args, listener); }
Example 20
Source File: Javac.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
public static int compile(String[] args, PrintWriter printWriter) { return Main.compile(args, printWriter); }