aspose file tools*
The moose likes Java in General and the fly likes Execute java statement stored in string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Execute java statement stored in string" Watch "Execute java statement stored in string" New topic
Author

Execute java statement stored in string

apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
Hi ,
This is my first question in the forum.
I have a java statement stored in a string variable.
Example:

String a="System.out.println(1)";

Is there any way to execute the command stored in string a so that i can see the output on console?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Ño you can't. The point is that java needs to be compiled to byte code to be able to be executed. Why do you want this?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Welcome to the Ranch

You could try writing the entire code of a class into a text file, giving it a .java extension, and using Runtime.exec() both to compile and execute that class.

But then, why on earth would you want to do that?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Take a look at Groovy (wikipedia article)

Bill
apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
Campbell Ritchie wrote:Welcome to the Ranch

You could try writing the entire code of a class into a text file, giving it a .java extension, and using Runtime.exec() both to compile and execute that class.

But then, why on earth would you want to do that?


Thanks.
I will try this. This reason i want this is to emulate something like Server side js. I would have used it but the code base is using java 1.5. So required some thing like this
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Assuming that js stands for JavaScript: you can run JavaScript directly on the JVM using the javax.scripting package. Look for example here
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

I think something like BeanShell or Groovy is as close as you'll get to what you're looking for.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
As far as i know, scripting is supported in java 1.6. As i already quoted Campbell Ritchie, my java is 1.5. And i dont have the option to upgrade it to 1.6.

apurav chauhan
Greenhorn

Joined: Aug 07, 2010
Posts: 21
Wouter Oet wrote:Assuming that js stands for JavaScript: you can run JavaScript directly on the JVM using the javax.scripting package. Look for example here


You are right Wouter. BSF is the right thing for me using java 1.5.

Thanks man.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
BSF is cool (assuming that it's JavaScript). If it's Java, then the Javassisst library can turn it into an executable class (all in memory, no file access needed).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute java statement stored in string