| java.lang.Object com.bostechcorp.cbesb.common.util.JavaUtils
JavaUtils | public class JavaUtils (Code) | | This class supports invoking Java compilation from within
a Java program. Recent versions of the Java environment have
provided such an API (in tools.jar). But that isn't available
on all platforms, and a fallback to the command line may be needed
(though this too may not always be available, eg. for security
reasons).
There's an additional complication in some environments --
such as Microsoft's VJ -- where the classpath as seen in
the System Properties may not be the one the user expects.
The code here is parameterized to try to deal with that.
|
Field Summary | |
protected static transient Log | logger |
Method Summary | |
public static boolean | JDKcompile(String fileName, String classPath, String dir) Try to compile a .java file on disk. | public static void | setDebug(boolean newDebug) Control whether compilation occurs with the -g option
(debugging information included in generated classfile).
This is an attribute, rather than a parameter on the compile
method, largely because it tends to be an all-or-nothing decision;
generally you're either doing program development and want it,
or running in production mode and don't. | static int | waitHardFor(java.lang.Process p) Subroutine: Like p.waitFor, but discards the InterruptedException
and goes right back into a wait. |
logger | protected static transient Log logger(Code) | | |
JDKcompile | public static boolean JDKcompile(String fileName, String classPath, String dir)(Code) | | Try to compile a .java file on disk. This will first attempt to
use the sun.java.tools.javac() method, then (if that is unavailable)
fall back upon shelling out to a command line and running javac
there.
NOTE: This must be _compiled_ with sun.java.tools.* (tools.jar)
available. We could change that to use reflection instead, if we
accept some overhead... minor compared to the cost of running the
compiler!
This has complications on some platforms. For example, under
Microsoft Visual Java (at least, as installed on my test system),
I found that I had to specify paths to both javac and xerces.jar
rather than counting on the shell's path and classpath having
been set to reach these. For that reason I've parameterized this
method with a few system properties, so you can adapt it to your
own system's needs without modifying the code:
- org.apache.xalan.utils.synthetic.javac
- Command line issued to invoke the compiler. Defaults to "javac",
which should work in most systems. In VJ, try setting it to
"cmd /c %JAVA_HOME%\\bin\javac.exe"
- org.apache.xalan.utils.synthetic.moreclasspath
- Additional classpath, to be prepended to the one retrieved from
java.class.path. Defaults to "" (empty). In VJ, try setting it to
point to your copy of xerces.jar, which may not be found otherwise.
TODO: Reconsider prepend versus append!
Parameters: String - fileName Which .java file to compile. Note that this maybe relative to the "current directory". Parameters: String - classPath Additional places to look for classes thatthis .java file depends upon. Becomes the javac command's-classpath parameter value. boolean True iff compilation succeeded. |
setDebug | public static void setDebug(boolean newDebug)(Code) | | Control whether compilation occurs with the -g option
(debugging information included in generated classfile).
This is an attribute, rather than a parameter on the compile
method, largely because it tends to be an all-or-nothing decision;
generally you're either doing program development and want it,
or running in production mode and don't. But that may not match
the needs of future users...
TODO: Consider whether debug should be a parameter.
boolean newDebug True to request debugging data,false to request optimized output. (It's uncommon towant both or neither!) |
waitHardFor | static int waitHardFor(java.lang.Process p)(Code) | | Subroutine: Like p.waitFor, but discards the InterruptedException
and goes right back into a wait. I don't want to report compiler
success or failure until it really has succeeded or failed... I think.
Parameters: Process - p to be waited for the exitValue() of the process. |
|
|