动态编译java_动态编译java源码

package com.mclon.jmxsgl.service.impl;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.IOException;

import java.io.OutputStream;

import java.net.URI;

import java.net.URL;

import java.net.URLClassLoader;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.tools.Diagnostic;

import javax.tools.FileObject;

import javax.tools.ForwardingJavaFileManager;

import javax.tools.JavaCompiler;

import javax.tools.JavaFileObject;

import javax.tools.SimpleJavaFileObject;

import javax.tools.StandardJavaFileManager;

import javax.tools.ToolProvider;

* 动态编译代码类

* @author zhangxu

public class DynamicEngine {

public static DynamicEngine getInstance() {

return new DynamicEngine();

private URLClassLoader parentClassLoader;

private DynamicEngine() {

this.parentClassLoader = (URLClassLoader) this.getClass()

.getClassLoader();

this.buildClassPath();

private void buildClassPath() {

if (classpath == null) {

StringBuilder sb = new StringBuilder();

Map filePath = new HashMap();

// 读取容器lib

try {

Enumeration urls = Thread.currentThread()

.getContextClassLoader().getResources(".");

String tomPath = urls.nextElement().getPath();

File file = new File(tomPath);

if (file.isDirectory()) {

File[] fList = file.listFiles();

for (int j = 0; j < fList.length; j++) {

if (fList[j].isFile()

&& (fList[j].getName().contains(".jar"))) {

fList[j].getPath();

filePath.put(fList[j].getName(),

tomPath + fList[j].getName());

} else if (file.getName().contains(".jar")) {

} catch (Exception e) {

System.out.println("Error: " + e);

// 读取工程lib

for (URL url : this.parentClassLoader.getURLs()) {

String p = url.getFile();

File file = new File(url.toString());

String fileName = file.getName();

filePath.put(fileName, p);

// 组装classpath

sb.append(filePath.get(key)).append(File.pathSeparator);

System.out.println(sb.toString());

this.classpath = sb.toString();

public Object javaCodeToObject(String fullClassName, String javaCode)

throws IllegalAccessException, InstantiationException {

long start = System.currentTimeMillis();

Object instance = null;

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

DiagnosticCollector diagnostics = new DiagnosticCollector();

ClassFileManager fileManager = new ClassFileManager(

compiler.getStandardFileManager(diagnostics, null, null));

List jfiles = new ArrayList();

jfiles.add(new CharSequenceJavaFileObject(fullClassName, javaCode));

List options = new ArrayList();

options.add("-encoding");

options.add("UTF-8");

options.add("-classpath");

options.add(this.classpath);

JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,

boolean success = task.call();

if (success) {

JavaClassObject jco = fileManager.getJavaClassObject();

DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(           this.parentClassLoader);http://www.huiyi8.com/moban/网页模板

Class clazz = dynamicClassLoader.loadClass(fullClassName, jco);

instance = clazz.newInstance();

} else {

String error = "";

for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {

error = error + compilePrint(diagnostic);

long end = System.currentTimeMillis();

System.out.println("javaCodeToObject use:" + (end - start) + "ms");

return instance;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值