File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Running Unix commands thru Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running Unix commands thru Java" Watch "Running Unix commands thru Java" New topic

Running Unix commands thru Java

SonalPSPL Bagmar

Joined: Jul 29, 2004
Posts: 22
HI all,
i want to run various Unix cmds thru a java prg. following is the code which i have written.

class Run
public static void main(String[] args)
String ls_str;
Process cmd_su = Runtime.getRuntime().exec("cd /users");

BufferedReader ls_in = new BufferedReader(new InputStreamReader(cmd_su.getInputStream()));

try {
while ((ls_str = ls_in.readLine()) != null) {
} catch (IOException e){


}catch(Exception e){
why is it that i cant see the dir changed to /users ?also the cmd
su (switch users) doesnt work !!!
any pointers will help me solving my problem ...

Thanks & Regards
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Hi SonalPSPL,

Simple answer: find a different way to do it.

More elaborate:
cd /users does nothing. The command cd changes cwd - current
working directory, which is an iternal variable of the shell
(or an application) currently running. Now let's have a look
at your exec("cd /users"). Right before this execution your
program will fork, which means that at that stage, 2 completly
same instances of the program will be running, with 2 completly
same yet independent contexts. In one of them, "cd /users" is
called. No matter if this cd finishes successfully, the process
doing it will finish straight after, the context (with the
possibly changed directory) will be thrown away, and that's it.
From my point of view it is working just right.

With su you are in the same situation but I guess there might
be even more problems with making su accept your input. If I
were su I would probably refuse to run if not started from
a terminal. Think about using sudo.

Best regards,

Get a better web browser:<br /><a href="" target="_blank" rel="nofollow"></a>
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Since you opened another thread to ask this same question, I'm not sure you understood Petr's explanation, so I'll try to give you a simpler version: the UNIX command "cd" only affects a shell that executes it, and it will have no effect on a Java process that executes it. The "su" command, on the other hand, starts a new shell altogether with the given user's privileges, and so again, the process that executed "su" is unaffected.

If your goal is to run a UNIX command as another user, then your best option would be to use the "sudo" command, as Petr suggested. If you don't have sudo available, then you can emulate it by using the "-c" option to su:

su - somebody -c "some command"

If you want a single shell to execute multiple commands as another user, then exec "su", use Process.getOutputStream() to get the standard input of the process, and send commands to it that way.

But in any event, there is no way to change the effective userid of the Java process itself without using native methods.

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
My suggestion is to use the correct tool for the job. This sounds like it would be better implemented as a shell script or C program rather than Java. Java does not easily allow the developer access to the underlying operating system. However, a shell script or C/C++ will.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Running Unix commands thru Java
It's not a secret anymore!