• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running Unix commands thru Java

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
-------------------------------------------------------------------
import java.io.*;

class Run
{
public static void main(String[] args)
{
String ls_str;
try{
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) {
System.out.println(ls_str);
}
} catch (IOException e){
System.exit(0);

}

}catch(Exception e){
e.printStackTrace();
}
}
}
--------------------------------------------------------------------
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
SONAL
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
Petr
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Layne
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic