This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Executing Java at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Executing Java at runtime" Watch "Executing Java at runtime" New topic

Executing Java at runtime

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585

I don't know what I'm going to ask is possible or not, but my case is the following:

Is there a way to have Java code in a String and to execute it?


And then executing this code without converting it to .java then . class!

Is there a way to do that?


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Henry Wong

Joined: Sep 28, 2004
Posts: 20374

If you are using Java 6, one option is the Java Compiler API. This allows you to call the classes used by the compiler to compile the classes without going through the awkward phase of finding javac and calling runtime.exec(). However, I believe this still generates the class files though.

Another option is to use the Java Scripting API. By default, it only does javascript, but there are a few java scripting engines available. So, you can load a java scipting engine, and use the java scripting API, to run your java, without compiling.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton

Joined: Sep 29, 2008
Posts: 12617
I agree. Here's the link:
subject: Executing Java at runtime
It's not a secret anymore!