BEA Systems, Inc.


weblogic.apache.xml.utils.synthetic
Class JavaUtils

java.lang.Object
  |
  +--weblogic.apache.xml.utils.synthetic.JavaUtils

public class JavaUtils
extends java.lang.Object

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.

This class is based on an implementation from the Apache XML Project. In future releases the XML parser, XSLT processor, and associated classes will likely be updated to be based on a later version of the Apache implementations. Since Apache does not guarantee backwards compatibility between versions of their software, we cannot guarantee backwards compatibility of any of the classes contained in the weblogic.apache package or sub-packages.


Constructor Summary
JavaUtils()
           
 
Method Summary
static boolean JDKcompile(java.lang.String fileName, java.lang.String classPath)
          Try to compile a .java file on disk.
static void setDebug(boolean newDebug)
          Control whether compilation occurs with the -g option (debugging information included in generated classfile).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaUtils

public JavaUtils()
Method Detail

setDebug

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. But that may not match the needs of future users...

TODO: Consider whether debug should be a parameter.

Parameters:
boolean - newDebug True to request debugging data, false to request optimized output. (It's uncommon to want both or neither!)

JDKcompile

public static boolean JDKcompile(java.lang.String fileName,
                                 java.lang.String classPath)
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:

weblogic.apache.xml.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"
weblogic.apache.xml.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 may be relative to the "current directory".
String - classPath Additional places to look for classes that this .java file depends upon. Becomes the javac command's -classpath parameter value.
Returns:
boolean True iff compilation succeeded.

Documentation is available at
http://download.oracle.com/docs/cd/E13222_01/wls/docs70

Copyright © 2004 BEA Systems, Inc. All Rights Reserved.
WebLogic Server 7.0 API Reference