Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execute java statement stored in string

 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ñ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?
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Groovy (wikipedia article)

Bill
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think something like BeanShell or Groovy is as close as you'll get to what you're looking for.
 
apurav chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic