aspose file tools*
The moose likes Beginning Java and the fly likes String execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String execution" Watch "String execution" New topic
Author

String execution

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
I have a String “h” and Its value is

String h=”System.out.println("HI");”

Now I want to execute the variable “h” which should print the value “HI”.


How Please explain how it can be done??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Don't understand the question. And it won't compile because you wrote " rather than \"
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

There is no easy way to do this in Java, because Java is a compiled language, not a scripting language which is executed directly from the source code. However, Java 6 does have an API for calling the compiler, see the package javax.tools.

You could also use BeanShell to do this.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

You want to dynamically execute the String
String h=”System.out.println(\"HI\");”

as a java command?

That is you want some way to say execute(h) and it should execute the SOP and print Hi?

I don't think that could be done. If I could execute arbitrary String as code during run time then the Java Bytecode verification would be moot - no point in doing byte code verification and allowing an arbitrary string to execute at runtime.

Why would you want to do something like that anyways?




Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

While you could do it in Java 6 (and, I suppose, lower versions by writing to a file and compiling manually), you'd be better off using a scripting language. There are several ways to go about that, and it's included in Java 6.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
A library like Javassist allows you to construct classes at runtime without having to create and compile files. You'd need a class each time you want to execute a piece of code, though.


Ping & DNS - my free Android networking tools app
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
because Java is a compiled language, not a scripting language


What are the difference between scripting language and programming language (java)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

a scripting language is usually something that runs programs on an OS, or another application. For example, you can write a unix script that goes something like

cd q $HOME\files
foreach $file ( @ files ) {
print "copying $file";
system ("rcp $file $dest") == 0 or die "it didn't work";
}

this script, when run, calls other programs used by the OS. you could easily enter these commands directly on the Unix prompt, and they'd run (except for the variables...).

Perl is another good example. It is trivial in Perl to wire together a bunch of OS commands.

According to the wikipeia, Firefox is written in C++, but can be controlled by the javascript scripting language.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: String execution