my dog learned polymorphism
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String execution" Watch "String execution" New topic

String execution

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 580
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

Joined: Oct 13, 2005
Posts: 46380
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: 15092

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

You could also use BeanShell to do this.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 607

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

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

Joined: Mar 22, 2005
Posts: 42965
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.
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 580
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

Joined: Oct 02, 2003
Posts: 11955

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
I agree. Here's the link:
subject: String execution
It's not a secret anymore!