[java] Scripting in Java

Started by
6 comments, last by nonnus29 19 years, 9 months ago
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++)
Advertisement
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 &#106avascript interpreter written in Java.<br><br>Mark
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.
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.

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 &#106avascript interpreter, written in java, that can compile &#106avascript to java classes.
Why do my programs never work on other computers?
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
- Artist on the web -- Lwjgl Ressources & blog -
Thanks for all the replys. I'll probably use something like pnuts or Dynamic Java (though, I have come across others since this).
Here's is a relavent tutorial from the sun site.

This topic is closed to new replies.

Advertisement