This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

String execution

 
Ranch Hand
Posts: 588
  • 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??
 
Marshal
Posts: 69799
277
  • 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 \"
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • 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?



 
Author
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.
 
Rancher
Posts: 43016
76
  • 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: 588
  • 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)
 
lowercase baba
Posts: 12871
62
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.
 
This tiny ad is suggesting that maybe she should go play in traffic.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic