本文整理了Java中javax.tools.JavaCompiler.run()
方法的一些代码示例,展示了JavaCompiler.run()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JavaCompiler.run()
方法的具体详情如下:
包路径:javax.tools.JavaCompiler
类名称:JavaCompiler
方法名:run
暂无
代码示例来源:origin: google/error-prone
@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
return javacTool.run(in, out, err, arguments);
}
代码示例来源:origin: stackoverflow.com
// Prepare source somehow.
String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"world\"); } }";
// Save source in .java file.
File root = new File("/java"); // On Windows running on C:\, this is C:\java.
File sourceFile = new File(root, "test/Test.java");
sourceFile.getParentFile().mkdirs();
Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
// Load and instantiate compiled class.
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("test.Test", true, classLoader); // Should print "hello".
Object instance = cls.newInstance(); // Should print "world".
System.out.println(instance); // Should print "test.Test@hashcode".
代码示例来源:origin: apache/flink
private static int compileClass(File sourceFile) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
return compiler.run(null, null, null, "-proc:none", sourceFile.getPath());
}
代码示例来源:origin: apache/flink
private static int compileClass(File sourceFile) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
return compiler.run(null, null, null, "-proc:none", sourceFile.getPath());
}
代码示例来源:origin: apache/geode
public List<CompiledSourceCode> compile(UncompiledSourceCode... uncompiledSources)
throws IOException {
File temporarySourcesDirectory = createSubdirectory(tempDir, "sources");
File temporaryClassesDirectory = createSubdirectory(tempDir, "classes");
List<String> options =
Stream.of("-d", temporaryClassesDirectory.getAbsolutePath(), "-classpath", classpath)
.collect(toList());
try {
for (UncompiledSourceCode sourceCode : uncompiledSources) {
File sourceFile = new File(temporarySourcesDirectory, sourceCode.simpleClassName + ".java");
FileUtils.writeStringToFile(sourceFile, sourceCode.sourceCode, Charsets.UTF_8);
options.add(sourceFile.getAbsolutePath());
}
int exitCode = ToolProvider.getSystemJavaCompiler().run(System.in, System.out, System.err,
options.toArray(new String[] {}));
if (exitCode != 0) {
throw new RuntimeException(
"Unable to compile the given source code. See System.err for details.");
}
List<CompiledSourceCode> compiledSourceCodes = new ArrayList<>();
addCompiledClasses(compiledSourceCodes, "", temporaryClassesDirectory);
return compiledSourceCodes;
} finally {
FileUtils.deleteDirectory(temporaryClassesDirectory);
}
}
代码示例来源:origin: cbeust/testng
private Class<?> compile(String src, String name) throws Exception {
// compile class and load it into by a custom classloader
File srcFile = new File(tmpDir, name + ".java");
try (PrintWriter pw = new PrintWriter(new FileWriter(srcFile))) {
pw.append(src);
}
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
assertEquals(0, javac.run(null, null, null, srcFile.getCanonicalPath()));
srcFile.delete();
File classFile = new File(tmpDir, name + ".class");
byte[] bytes;
try (DataInputStream dis = new DataInputStream(new FileInputStream(classFile))) {
bytes = new byte[dis.available()];
dis.readFully(bytes);
}
classFile.delete();
return defineClass(name, bytes, 0, bytes.length);
}
代码示例来源:origin: kilim/kilim
compiler.run(null, null, null, arguments);
代码示例来源:origin: apache/hbase
/**
* Compiles the test class with bogus code into a .class file.
* Unfortunately it's very tedious.
* @param counter Unique test counter.
* @param packageNameSuffix Package name suffix (e.g. ".suffix") for nesting, or "".
* @return The resulting .class file and the location in jar it is supposed to go to.
*/
private static FileAndPath compileTestClass(long counter,
String packageNameSuffix, String classNamePrefix) throws Exception {
classNamePrefix = classNamePrefix + counter;
String packageName = makePackageName(packageNameSuffix, counter);
String javaPath = basePath + classNamePrefix + ".java";
String classPath = basePath + classNamePrefix + ".class";
PrintStream source = new PrintStream(javaPath);
source.println("package " + packageName + ";");
source.println("public class " + classNamePrefix
+ " { public static void main(String[] args) { } };");
source.close();
JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
int result = jc.run(null, null, null, javaPath);
assertEquals(0, result);
File classFile = new File(classPath);
assertTrue(classFile.exists());
return new FileAndPath(packageName.replace('.', '/') + '/', classFile);
}
代码示例来源:origin: didi/DDMQ
private void compile(String[] srcFiles) throws Exception {
String args[] = this.buildCompileJavacArgs(srcFiles);
ByteArrayOutputStream err = new ByteArrayOutputStream();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new NullPointerException(
"ToolProvider.getSystemJavaCompiler() return null,please use JDK replace JRE!");
}
int resultCode = compiler.run(null, null, err, args);
if (resultCode != 0) {
throw new Exception(err.toString(RemotingHelper.DEFAULT_CHARSET));
}
}
代码示例来源:origin: stackoverflow.com
String convert = "public class NewClass { public static void main(String[] args) { System.out.println(\"test\"); }}";
String filename = "MyClass.java";
int i = convert.indexOf(" class ");
if(i == -1) {
convert = "public class MyClass { " + convert + " } ";
} else {
int classNameIndex = convert.indexOf(" ", i+1);
int classNameIndexEnd = convert.indexOf(" ", classNameIndex+1);
filename = convert.substring(classNameIndex+1, classNameIndexEnd) + ".java";
}
PrintWriter wr = response.getWriter();
File f = new File(filename);
if (!f.exists()) {
f.createNewFile();
wr.println("File is created\n");
}
FileWriter write = new FileWriter(f);
BufferedWriter bf = new BufferedWriter(write);
bf.write(convert);
bf.close();
wr.println("File writing done\n");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, filename);
if (result == 0) {
wr.println("Compilation Successful");
} else {
wr.println("Compilation Failed");
}
代码示例来源:origin: kohsuke/args4j
public int run(String[] args) throws Exception {
CmdLineParser parser = new CmdLineParser(this);
try {
parser.parseArgument(args);
} catch (CmdLineException e) {
System.err.println(e.getMessage());
printUsage(parser);
return -1;
}
if(aptArgs.isEmpty()) {
printUsage(parser);
return 0;
}
// we'll use a separate class loader to reload our classes,
// so parameters need to be set as system properties. Ouch!
System.setProperty("args4j.outdir",outDir.getPath());
System.setProperty("args4j.format",mode.name());
if(resourceName==null) resourceName = ""; // can't have null in properties
System.setProperty("args4j.resource",resourceName);
aptArgs.add(0, "-proc:only");
aptArgs.add(1, "-processor");
aptArgs.add(2, AnnotationProcessorImpl.class.getName());
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
return javac.run(System.in, System.out, System.err, aptArgs.toArray(new String[0]));
}
代码示例来源:origin: takari/polyglot-maven
compiler.run(null, null, null, "-parameters", "-classpath", getClassPath(), filePath);
log.debug("Dynamically compiled class " + filePath);
代码示例来源:origin: Netflix/hollow
/**
* Compiles java source files in the provided source directory, to the provided class directory.
* This also runs findbugs on the compiled classes, throwing an exception if findbugs fails.
*/
public static void compileSrcFiles(String sourceDirPath, String classDirPath) throws Exception {
List<String> srcFiles = new ArrayList<>();
addAllJavaFiles(new File(sourceDirPath), srcFiles);
File classDir = new File(classDirPath);
classDir.mkdir();
List<String> argList = new ArrayList<>();
argList.add("-d");
argList.add(classDir.getAbsolutePath());
argList.add("-classpath");
argList.add(System.getProperty(PROPERTY_CLASSPATH) + System.getProperty("path.separator") + classDirPath);
argList.addAll(srcFiles);
// argList.toArray() for large size had trouble
String[] args = new String[argList.size()];
for (int i = 0; i < argList.size(); i++) {
args[i] = argList.get(i);
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int err = compiler.run(null, System.out, System.out, args);
if (err != 0)
throw new RuntimeException("compiler errors, see system.out");
runFindbugs(classDir);
}
代码示例来源:origin: apache/phoenix
int result = jc.run(null, null, null, javaFileName);
assertEquals(0, result);
代码示例来源:origin: errai/errai
@Override
public int compile(final OutputStream out, final OutputStream errors, final String outputPath, final String toCompile, final String classpath) {
return compiler.run(null, out, errors, "-classpath", classpath, "-d", outputPath, toCompile);
}
}
代码示例来源:origin: org.jboss.errai/errai-codegen
@Override
public int compile(final OutputStream out, final OutputStream errors, final String outputPath, final String toCompile, final String classpath) {
return compiler.run(null, out, errors, "-classpath", classpath, "-d", outputPath, toCompile);
}
}
代码示例来源:origin: stackoverflow.com
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);
if (compilationResult == 0) {
System.out.println("Compilation is successful");
} else {
System.out.println("Compilation Failed");
}
代码示例来源:origin: stackoverflow.com
...
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter which class:");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
className=br.readLine()
int results = compiler.run(null, null, null, className+".java");
if(results == 0){
Class clazz = Class.forName(className+".class");
System.out.print("Compiled successfully.Enter which method:");
Object returnValue=clazz.getMethod(br.readLine()).invoke(null);
}
...
代码示例来源:origin: stackoverflow.com
public static void main(String... args) throws Exception {
String source = "public class Test { static { System.out.println(\"test\"); } }";
File root = new File("/test");
File sourceFile = new File(root, "Test.java");
Writer writer = new FileWriter(sourceFile);
writer.write(source);
writer.close();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("Test", true, classLoader);
}
代码示例来源:origin: com.mysema.querydsl/querydsl-sql-codegen
private void compile(MetaDataExporter exporter) {
JavaCompiler compiler = new SimpleCompiler();
Set<String> classes = exporter.getClasses();
int compilationResult = compiler.run(null, null, null, classes.toArray(new String[classes.size()]));
if(compilationResult == 0) {
System.out.println("Compilation is successful");
} else {
Assert.fail("Compilation Failed");
}
}
内容来源于网络,如有侵权,请联系作者删除!