一、前言
关于apache的bsf源码包中的org.apache.bsf.util.JavaUtils工具类,对指定类文件进行编译生成二进制class目标文件JDKcompile,详情见源码说明。
二、源码说明
package org.apache.bsf.util;@b@@b@import java.io.IOException;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public class JavaUtils@b@{@b@ private static Log logger = LogFactory.getLog(JavaUtils.class.getName());@b@@b@ public static boolean JDKcompile(String fileName, String classPath)@b@ {@b@ String option = (logger.isDebugEnabled()) ? "-g" : "-O";@b@ String[] args = { "javac", option, "-classpath", classPath, fileName };@b@@b@ logger.debug("JavaEngine: Compiling " + fileName);@b@ logger.debug("JavaEngine: Classpath is " + classPath);@b@ try@b@ {@b@ Process p = Runtime.getRuntime().exec(args);@b@ p.waitFor();@b@ return (p.exitValue() != 0);@b@ } catch (IOException e) {@b@ logger.error("ERROR: IO exception during exec(javac).", e);@b@ } catch (SecurityException e) {@b@ logger.error("ERROR: Unable to create subprocess to exec(javac).", e);@b@ }@b@ catch (InterruptedException e) {@b@ logger.error("ERROR: Wait for exec(javac) was interrupted.", e);@b@ }@b@ return false;@b@ }@b@}