This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Running Unix commands thru Java  RSS feed

SonalPSPL Bagmar
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Ernest Friedman-Hill
author and iconoclast
Posts: 24215
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!