Sign in to follow this  
MetaCipher

[java] Scripting in Java

Recommended Posts

MetaCipher    145
I am sure Java has the support to do this (or I hope it does), where you can have a Java App load .java and execute them like scripts. (Much like using Lua with C++)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You can load and unload classes at runtime, but you can't load .java files.

On the other hand, you can execute the compiler (if present in your environment) to compile .java files into classes before loading them.

This is what JSP does.

In a simplistic way, execute javac with the relevant parameters, add the output into your classpath at runtime, and use Class.forName() to load the class you want (by name).

The problem with this, is that it's difficult to unload the class. To unload classes, I think you have to create your own classloader. This isn't that tricky and it is documented.

You could also investigate Rhino, a javascript interpreter written in Java.

Mark

Share this post


Link to post
Share on other sites
Aldacron    4544
The problem with compiling on the fly is that, for now, you have to access the com.sun.java packages. Anytime you use those you are asking for future headaches, particularly if a user runs your app on a non-Sun JRE. Java 1.5, err, 5.0 as they now call it, was originally supposed to include a compiler API in the standard library, but it still hasn't made it into the API yet as of beta 2.

In the meantime, pnuts is the next best thing. Pnuts scripts can be interpreted, or compiled to Java bytecode at runtime and loaded as classes. Additionally, any Java classes which implement a particular pnuts interface are interchangeable with the scripts. You will likely find pnuts to be a much better solution than Rhino, Beanshell, Jython, or any of the other Java scripting solutions out there.

Share this post


Link to post
Share on other sites
nmi    978
Quote:
Original post by Anonymous Poster
You can load and unload classes at runtime, but you can't load .java files.


The JAVA VM specification says:

2.17.8 Unloading of Classes and Interfaces
A class or interface may be unloaded if and only if its class loader is unreachable. The bootstrap class loader is always reachable; as a result, system classes may never be unloaded.

You cannot manually unload classes, you can just make their classloader unreachable. Then the VM is free to unload classes.

Quote:

On the other hand, you can execute the compiler (if present in your environment) to compile .java files into classes before loading them.


To compile classes at runtime you can call

sun.tools.javac.Main.main(args);

However this class does not belong to the standard API, so you are consrained to a specific version of a JVM from Sun. To use this class you will need an installed JDK. Another way would be calling javac like any other program, which involves creating a new VM, is somewhat slower and uses more memory.

Quote:

In a simplistic way, execute javac with the relevant parameters, add the output into your classpath at runtime, and use Class.forName() to load the class you want (by name).


right

But you may also load the generated class and call defineClass(byte[] b, int off, int len) on the java.lang.ClassLoader (you must derive your own ClassLoader to do so). This frees you from putting the class in a directory that classpath contains. You can put your class anywhere.

Share this post


Link to post
Share on other sites
Koroljov    115
You can allways write your own compiler, compile the .java files, and then use defineClass(byte[] b, int off, int len).

You should have a look at rhino (http://www.mozilla.org/rhino/). It is a javascript interpreter, written in java, that can compile javascript to java classes.

Share this post


Link to post
Share on other sites
Chman_a11w    128
Quote:
Original post by Anonymous Poster
You can load and unload classes at runtime, but you can't load .java files.


Wonrg, you can ! Simply use DynamicJava ! I've worked with ot a little time ago and it works like a charm !

Here it is : Clicky

Hope it helps!

Chman

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this