java 关联tomcat_Tomcat Server 是如何关联 JRE 和 JDK 的?

import org.apache.commons.io.IOUtils;

import org.eclipse.jdt.internal.compiler.*;

import org.eclipse.jdt.internal.compiler.Compiler;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;

import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;

import org.eclipse.jdt.internal.compiler.env.INameEnvironment;

import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;

import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintStream;

import java.util.HashMap;

import java.util.Locale;

public class TestIt {

public static String code = "package a;" +

"public class Abc {" +

" public static void main(String[] args) {" +

" System.out.println(\"aaabbbccc\");" +

" }" +

"}";

static ICompilerRequestor compilerRequestor = new ICompilerRequestor() {

@Override

public void acceptResult(CompilationResult result) {

if (result.hasErrors()) {

System.out.println("编译出现错误");

System.out.println(result.getErrors()[0].toString());

return;

}

ClassFile[] classFiles = result.getClassFiles();

ClassFile classFile = classFiles[0];

try {

new PrintStream("C:\\Users\\xinxi\\Desktop\\a\\Abc.class").write(classFile.getBytes());

} catch (IOException e) {

e.printStackTrace();

}

}

};

static INameEnvironment nameEnvironment = new INameEnvironment() {

public NameEnvironmentAnswer findType(final char[][] compoundTypeName) {

return findType(join(compoundTypeName));

}

public NameEnvironmentAnswer findType(final char[] typeName, final char[][] packageName) {

return findType(join(packageName) + "." + new String(typeName));

}

private NameEnvironmentAnswer findType(final String name) {

try {

if (name.equals("a.Abc")) {

ClassFileReader classFileReader = new ClassFileReader(code.getBytes(), name.toCharArray(), true);

return new NameEnvironmentAnswer(classFileReader, null);

}

InputStream input = this.getClass().getClassLoader().getResourceAsStream(name.replace(".", "/") + ".class");

if (input != null) {

byte[] bytes = IOUtils.toByteArray(input);

if (bytes != null) {

ClassFileReader classFileReader = new ClassFileReader(bytes, name.toCharArray(), true);

return new NameEnvironmentAnswer(classFileReader, null);

}

}

} catch (Exception e) {

throw new RuntimeException(e);

}

return null;

}

public boolean isPackage(char[][] parentPackageName, char[] packageName) {

String name = new String(packageName);

if (parentPackageName != null) {

name = join(parentPackageName) + "." + name;

}

if (name.equals("a.Abc")) {

return false;

}

return true;

}

public void cleanup() {

}

};

private static class StringCompilationUnit implements ICompilationUnit {

private String fileName;

private char[] typeName;

private char[][] packageName;

private String content;

public StringCompilationUnit(String className, String content) {

this.content = content;

if (className.contains("$")) {

className = className.substring(0, className.indexOf("$"));

}

fileName = className.replace('.', '/') + ".java";

int pos = className.lastIndexOf('.');

if (pos > 0) {

typeName = className.substring(pos + 1).toCharArray();

} else {

typeName = className.toCharArray();

}

String[] names = className.split("\\.");

packageName = new char[names.length - 1][];

for (int i = 0; i < packageName.length; i++) {

packageName[i] = names[i].toCharArray();

}

}

@Override

public char[] getContents() {

return content.toCharArray();

}

@Override

public char[] getMainTypeName() {

return typeName;

}

@Override

public char[][] getPackageName() {

return packageName;

}

@Override

public boolean ignoreOptionalProblems() {

return false;

}

@Override

public char[] getFileName() {

return fileName.toCharArray();

}

}

public static void main(String[] args) {

IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);

IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();

CompilerOptions options = new CompilerOptions(new HashMap() {{

put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_8);

put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_8);

}});

Compiler jdtCompiler = new Compiler(nameEnvironment, policy, options, compilerRequestor, problemFactory);

jdtCompiler.compile(new ICompilationUnit[]{new StringCompilationUnit("a.Abc", code)});

}

private static String join(char[][] chars) {

StringBuilder sb = new StringBuilder();

for (char[] item : chars) {

if (sb.length() > 0) {

sb.append(".");

}

sb.append(item);

}

return sb.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值