aspose file tools*
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 Spring in Action 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
Author

Executing Java at runtime

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hello,

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?

Example:



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

Is there a way to do that?

Thanks,

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

Joined: Sep 28, 2004
Posts: 18896
    
  40


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.

Henry


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

Joined: Sep 29, 2008
Posts: 12617

http://www.csg.is.titech.ac.jp/~chiba/javassist/
 
Consider Paul's rocket mass heater.
 
subject: Executing Java at runtime