Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String execution

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand the question. And it won't compile because you wrote " rather than \"
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15440
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic