my dog learned polymorphism*
The moose likes Linux / UNIX and the fly likes Standard way of using Linux command using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Standard way of using Linux command using java" Watch "Standard way of using Linux command using java" New topic
Author

Standard way of using Linux command using java

swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
Hi,

In application I'm using java.lang.Process to execute the Linux command.
Is it the only way to call Linux command through java.

Please help to choose proper way for execution of command.

thanks


Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
The standard way involves java.lang.Process, but you cannot use that directly.
The Process instance is created by java.lang.ProcessBuilder, which can be invoked directly or (more common, I think) via one of the java.lang.Runtime.exec(...) overloads.
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
yup the way you are saying is correct.
but is there any other way because I have to use it many times
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
You mean, you need to run a given command several times -- start several subprocesses ? For that, ProcessBuilder is the best choice. It creates a Process instance that you can start() as many times as you need and it will generate a new subprocess every time with the same command, environment etc.
UPDATE: sorry, it's not the process that you reuse, it's the builder itself: each call to start() creates a new Process with the same command, env...
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
is there any way to set timeout for this... if I used waitFor() then it will stop my application....
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
Use waitFor() from a different thread.
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
but we are doing same thing by using waitFor() from different thread...
I want to implement timeout concept for waitFor() as if that method fails then one should recover it.
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
Than you could start an extra thread that simply sleep()s for an amount of time and then checks if the Process is still running, and destroy()s it if so. Or a combination of Process.waitFor(), Thread.sleep(), Thread.interrupt() in two separate threads.
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
solution you have mentioned took lots of resource and also make application slow...
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
I haven't provided a solution, but merely a place to start looking. Could you show me the relevant parts of the implementation ?
swapnel surade
Ranch Hand

Joined: Mar 05, 2009
Posts: 129
I want to say that the way you have mentioned can take more resources so don't want to implement in that way...
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
Well, you don't need to spawn two threads for each created process. Actually, I think you can reduce it all to only one extra thread that monitors all the spawned processes in a clever way and kills them if they've lasted for too long.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Hello Swapnel,

Maybe you can tell us more about the problem you try to solve.

How many linuxcommands do you need to run (in which timespan)? How do they look like?

If you have many commands, you might collect them in a script, and execute that script instead, if you can schedule the execution a bit.

Or, if it is allways the same command with slightly different parameters - maybe you can implement it in java?


http://home.arcor.de/hirnstrom/bewerbung
 
Don't get me started about those stupid light bulbs.
 
subject: Standard way of using Linux command using java