Skip to main content
gamedev.net
🔒 Locked

[java] Dynamic Code Generation in Java

Started by Enfekted 7 replies 1.5k views
Original Post
Enfekted
Enfekted
Hello, For the past year or so, I've been using .NET to do most all of my programming because of its ease in UI development. But now I'm starting a new project and have decided to use Java. However, this new project is dependent upon the inclusion of a dynamic code generator and compiler pre-existing in the libraries (don't want to have to develop or pay for it). In .NET I've been using the ICodeGenerator and ICodeCompiler classes to compile source strings into an assembly in memory that I can easily create instances of using the Assembly class methods. I'd assumed that since .NET is somewhat of a replica of the java libraries that java had this functionality also, but after searching for a couple hours have thought that maybe the functionality does not already exist. I looked at JBind, but it doesn't look like that has the functionality to execute classes after compilation. Am I just overlooking the classes I need or did .NET actually come out with something unique? [Edited by - Enfekted on December 19, 2004 9:39:59 AM]
Enfekted
Enfekted
Just to make sure I'm clear on what I'm trying to do, here's an snippet of basic dynamic code compilation and execution in C#

string source = "using System;\n" +	"namespace MyNamespace\n" +	"{\n" + 	"\tpublic class MyClass\n" +	"\t{\n" +	"\t\tpublic void HelloWorld()\n" +	"\t\t{\n" +	"\t\t\tConsole.WriteLine( \"Dynamic call to HelloWorld()\\n\\\"Hello World!!!\\\"\" );\n" +	"\t\t}\n" +	"\t}\n" + 	"}\n";CSharpCodeProvider cscp = new CSharpCodeProvider();ICodeCompiler compiler = cscp.CreateCompiler();CompilerParameters parameters = new CompilerParameters();parameters.GenerateExecutable = false;parameters.GenerateInMemory = true;CompilerResults results = compiler.CompileAssemblyFromSource( parameters, source );object obj = results.CompiledAssembly.CreateInstance( "MyNamespace.MyClass" );obj.GetType().GetMethod( "HelloWorld" ).Invoke( obj, null );
markr
markr
You can certainly do it by invoking javac on .java files you've dynamically written out.

You can compile them into a temporary directory, create a ClassLoader which loads classes from there, use that classloader to load the classes and then do whatever you want with them.

If you need to change your code then, throw away the old ClassLoader instance, recompile the code and create a new ClassLoader for the new code. You can do this without exiting your program.

I may be wrong, but I heard that that's effectively all the CSharp code compiler thing does anyway.

Mark
nmi
nmi
What you are looking for is:

com.sun.tools.javac.Main.compile(String[] lines)

You can find an article on this site.
Enfekted
Enfekted
Thanks, nmi! That's exactly what i was looking for.

And that wouldn't have been a bad idea either markr.

Thanks!
capn_midnight
capn_midnight
Quote:
Original post by nmi
What you are looking for is:

com.sun.tools.javac.Main.compile(String[] lines)

You can find an article on this site.

ah, yes, the undocumented stuff.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.