aspose file tools*
The moose likes Java in General and the fly likes Java 1.6 Scripting Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java 1.6 Scripting Query" Watch "Java 1.6 Scripting Query" New topic
Author

Java 1.6 Scripting Query

Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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.....


Java Programmer | SCJP 1.5 | SCWCD 1.4
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
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


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please don't write "hth" or similar.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Jun 26, 2007
Posts: 136
I understand. Thank you Lester for the link.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 1.6 Scripting Query