• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Java 1.6 Scripting Query

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the java6 scripting feature we can use java script functions from java code. Using the following code

i am obtaining the script engine. Is there any script engine present to execute unix shell scripts? If present then how to configure it with the existing java env. ?
Thank you in advance.....
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can execute shell commands and script files directly from Java, using java.lang.Runtime. It's a bit more complex than it seems, search this forum or internet for in depth explanations.

hth

Martin
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Martin. That is one way but that will be platform specific.
I am trying to do something by using the java 1.6 ScriptManager where we can get different script engine and execute respective script.
 
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to execute a Unix shell script and expect it to be platform independent?

If you want it to be platform independent you may want to try translating the code to Java, or possibly use Python/Jython, Groovy or something similar.
 
Marshal
Posts: 79800
385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't write "hth" or similar.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
To explain my point I am giving the following example.

The above test code uses the following codes.js file

Once I run the code It will print 21.
Now my question is that instead of this .js file if I have a unix shell script which contains some function then is there a way to execute in the same way? For that we require a separate ScriptEngine. So is there already some script engine present?

Once again thank you for response.

 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No shell script engine exists to my knowledge (see https://scripting.dev.java.net/ for an incomplete list of available engines). Such an engine would also face some practical difficulties as there are numerous different shells, each with its own syntax.

The best approach is probably what Martin suggested: use Runtime.exec or java.lang.ProcessBuilder.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand. Thank you Lester for the link.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would be interesting, though... aside from some really OS-specific stuff (like nohup and devices etc.) I don't see why it couldn't be done, assuming Java implementations of the likely suspects. Hmm.
 
Rob Spoor
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is, shell scripts are full of built-in commands and calls to other tools. In the end, unless you limit yourself to only built-in commands, you're going to need ProcessBuilder at some time. Instead of managing the input / output of all those processes you might as well use only one external process, to the shell script.

Which brings us to When Runtime.exec() won't.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I'm saying if the usual suspects had internal Java implementations. Some already do, like grep. It'd be fairly straight-forward to provide the other common ones, like sed, awk, cut, etc. The shell language itself would be easy(ish). Not saying it's a good idea, just saying it's doable, and interesting.
 
Would you like to try a free sample? Today we are featuring tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic