当前位置: 首页>>代码示例>>Java>>正文


Java JavaCompiler.run方法代码示例

本文整理汇总了Java中javax.tools.JavaCompiler.run方法的典型用法代码示例。如果您正苦于以下问题:Java JavaCompiler.run方法的具体用法?Java JavaCompiler.run怎么用?Java JavaCompiler.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.tools.JavaCompiler的用法示例。


在下文中一共展示了JavaCompiler.run方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createTestClass

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
static void createTestClass() throws IOException {
    FileOutputStream fos = new FileOutputStream(TESTFILE + ".java");
    PrintStream ps = new PrintStream(fos);
    ps.println("public class " + TESTFILE + "{");
    ps.println("public static void main(String[] args) {\n");
    ps.println("System.out.println(System.getProperty(\"sun.boot.library.path\"));\n");
    ps.println("}}\n");
    ps.close();
    fos.close();

    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    String javacOpts[] = {TESTFILE + ".java"};
    if (javac.run(null, null, null,  javacOpts) != 0) {
        throw new RuntimeException("compilation of " + TESTFILE + ".java Failed");
    }
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:17,代码来源:TestBootNativeLibraryPath.java

示例2: compileTestClass

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:26,代码来源:TestClassFinder.java

示例3: runJavac

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
/**
 * Run the Java compiler.
 * (A JSR 199 implementation must be available.)
 * @param src a source root (runs javac on all *.java it finds matching {@code srcIncludes})
 * @param srcIncludes a pattern of source files names without path to compile (useful for testing incremental compiles), or null for all
 * @param dest a dest dir to compile classes to
 * @param cp classpath entries; if null, use Java classpath of test
 * @param stderr output stream to print messages to, or null for test console (i.e. do not capture)
 * @return true if compilation succeeded, false if it failed
 */
public static boolean runJavac(File src, String srcIncludes, File dest, File[] cp, OutputStream stderr) {
    List<String> args = new ArrayList<String>();
    args.add("-classpath");
    StringBuilder b = new StringBuilder(dest.getAbsolutePath());
    if (cp != null) {
        for (File entry : cp) {
            b.append(File.pathSeparatorChar);
            b.append(entry.getAbsolutePath());
        }
    } else {
        b.append(File.pathSeparatorChar).append(System.getProperty("java.class.path"));
    }
    args.add(b.toString());
    args.add("-d");
    args.add(dest.getAbsolutePath());
    args.add("-sourcepath");
    args.add(src.getAbsolutePath());
    args.add("-s");
    File destG = new File(dest.getParentFile(), "generated-" + dest.getName());
    args.add(destG.getAbsolutePath());
    args.add("-source");
    args.add("6");
    args.add("-Xlint:-options");
    dest.mkdirs();
    destG.mkdirs();
    scan(args, src, srcIncludes);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    Assert.assertNotNull(String.format(
            "No JSR 199 compiler impl found; perhaps tools.jar missing from CP? BootClassPath: %s. ClassPath: %s",
            System.getProperty("sun.boot.class.path"),  //NOI18N
            System.getProperty("java.class.path")       //NOI18N
            ),
        compiler);
    //System.err.println("running javac with args: " + args);
    return compiler.run(null, null, stderr, args.toArray(new String[args.size()])) == 0;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:47,代码来源:AnnotationProcessorTestUtils.java

示例4: testCompilation

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
@Test
public void testCompilation() throws Exception {
    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    int result = compiler.run(null, null, null, output.toString());
    Assert.assertEquals("Compiling the Schema failed", 0, result);

}
 
开发者ID:ansell,项目名称:rdf4j-schema-generator,代码行数:9,代码来源:AbstractSchemaSpecificTest.java

示例5: testSchemaCompilation

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
@Test
public void testSchemaCompilation() throws ClassNotFoundException, MalformedURLException {
    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    int result = compiler.run(null, null, null, output.toString());
    Assert.assertEquals("Compiling the Vocab failed", 0, result);

}
 
开发者ID:ansell,项目名称:rdf4j-schema-generator,代码行数:9,代码来源:SchemaGeneratorCompileTest.java

示例6: generateSources

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
@Test
public void generateSources() throws Exception {
    Path directory = Paths.get("target/generated-sources/log/");

	JavaGenerator.generate(Paths.get("src/test/resources/yaml/agresso.yaml"), directory);
	JavaGenerator.generate(Paths.get("src/test/resources/yaml/global.yaml"), directory);
	JavaGenerator.generate(Paths.get("src/test/resources/yaml/network.yaml"), directory);
	
	// verify that the generated classes can be loaded
	for(String domain : new String[]{"agresso", "global", "network"}) {
		File network = new File(String.format("target/generated-sources/log/com/example/%s/", domain));
		File[] results = network.listFiles(new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(".java");
			}
		});

		List<String> files = new ArrayList<>();
		for(File result : results) {
			files.add(result.getAbsolutePath());
		}

	    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
	    int status = compiler.run(null, null, null, files.toArray(new String[files.size()]));
	    if(status != 0) {
	    	throw new IllegalArgumentException();
	    }			
	}
}
 
开发者ID:skjolber,项目名称:json-log-domain,代码行数:30,代码来源:YamlTest.java

示例7: compile

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
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));
    }
}
 
开发者ID:lirenzuo,项目名称:rocketmq-rocketmq-all-4.1.0-incubating,代码行数:14,代码来源:DynaCode.java

示例8: compile

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
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());
    }
}
 
开发者ID:y123456yz,项目名称:reading-and-annotate-rocketmq-3.4.6,代码行数:14,代码来源:DynaCode.java

示例9: compile

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
/**
 * Given a list of file-scope java code (equivalent to a .java file, including package and
 * import declarations), compile() invokes javac to compile them, produce classfiles and return
 * a list of <className, byte[]> pairs.
 * <p>
 * compile() dumps the source strings into their respective files, has javac compile them, then
 * reads back the equivalent class files. The name of the source file is gleaned from the string
 * itself; a string containing "public class Foo" is stored in tmpDir/Foo.java (where tmpDir is
 * a temporary directory that's deleted after the compilation), and if no public class or
 * interface is found, the name of the first class in the string is used.
 * <p>
 * Note that the list of returned classes may be larger than
 *
 * @param srcCodes . List of strings.
 * @return List<className,byte[]>. className is fully qualified, and byte[] contains the
 * bytecode of the class.
 * @throws IOException
 */
public static List<ClassInfo> compile(List<String> srcCodes) throws IOException {

  List<SourceInfo> srcInfos = getSourceInfos(srcCodes);

  File rootDir = getTmpDir(); // something like "/tmp/kilim$2348983948"

  File classDir = new File(rootDir.getAbsolutePath() + File.separatorChar + "classes");
  classDir.mkdir(); // "<rootDir>/classes"

  String options[] = {"-d", classDir.getAbsolutePath()};

  String args[] = new String[options.length + srcCodes.size()];
  System.arraycopy(options, 0, args, 0, options.length);
  int i = options.length;

  for (SourceInfo srci : srcInfos) {
    String name = rootDir.getAbsolutePath() + File.separatorChar + srci.className + ".java";
    writeFile(new File(name), srci.srcCode.getBytes());
    args[i++] = name;
  }

  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  compiler.run(null, null, null, args);

  List<ClassInfo> ret = new ArrayList<ClassInfo>();
  addClasses(ret, "", classDir);
  deleteDir(rootDir);
  return ret;
}
 
开发者ID:kogupta,项目名称:scala-playground,代码行数:48,代码来源:Javac.java

示例10: main

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
public static void main(String[] args) {
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    javac.run(System.in, null, null,
              "-Xprint",
              "com.sun.tools.javac.code.Types",
              "com.sun.tools.javac.parser.Parser",
              "java.util.EnumSet");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:Xprint.java

示例11: compile

import javax.tools.JavaCompiler; //导入方法依赖的package包/类
static boolean compile(String[] args, OutputStream out, ErrorReceiver receiver){
    try {
        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
        if (comp == null) {
            receiver.error(JavacompilerMessages.NO_JAVACOMPILER_ERROR(), null);
            return false;
        }
        return 0 == comp.run(null, out, out, args);
    } catch (SecurityException e) {
        receiver.error(e);
    }
    return false;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:JavaCompilerHelper.java


注:本文中的javax.tools.JavaCompiler.run方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。