Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 1.6 Scripting Query

 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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.....
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't write "hth" or similar.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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.

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I understand. Thank you Lester for the link.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic