java - JavaCompiler from JDK 1.6: how to write class bytes directly to byte[] array? -
That's why I recently provided the new information available in JDK 1.6. This code makes it easy to directly compile a string
with a .class
file:
string className = "Foo" ; String source code = "..."; JavaCompiler Compiler = Toolpace .getSystemJavaCompiler (); & Lt; JavaSourceFromString & gt; UnitsToCompile = New Arrestist & lt; JavaSourceFromString & gt; () {{Add (new javasuresframe string (classname, source code)); }}; Standard java file manager file manager = compiler.getStandardFileManager (empty, null, empty); Compiler.getTask (empty, file manager, blank, blank, empty units compile) .col (); FileManager.close (); Bytereon OutputStream Boss = New Bytereunootstream (); FileInputStream fis = New FileInputStream (className + ".class"); IOUtils.copyStream (Fis, boss); Return bos.toByteArray ();
You can catch the source with JavaSourceFromString
.
This is very easily sourceCode
to Foo.class
in current working directory
My question is : Is it possible to directly compile it into the byte []
array, and file
I / O completely?
Maybe you can create your own javax.tools JavaFileManager
class To implement, where you will return your own implementation of javax.tools.FileObject
, which will then write it instead of the disk. Therefore throws IOException for your subclass of javax.tools.FileObject
Writer Opener ()
the method that you will return to java.io.StringWriter
. All methods should be converted to their string
counterparts.
Comments
Post a Comment