aspose file tools*
The moose likes Java in General and the fly likes More Runtime questions 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 "More Runtime questions" Watch "More Runtime questions" New topic
Author

More Runtime questions

Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
Hello. I'm trying to use the Runtime class to execute some functions in a unix shell.

I want to be able to run two processes keeping the environment the same environment eg:
" su - myusername "
"somecommand that user can do"

The problem using : Process p = Runtime.getRuntime.exec("su - myusername"); is that the environment is lost when I run:
Process q = getRuntime.exec("somecommand that user can do");
Or at least I think it is. Any ideas?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

When you execute "su", the process that is started is an interactive shell. So in theory, what you could do would be to start the "su" process, then send subsequent commands to that shell's standard input -- i.e., call Process.getOutputStream() and print the commands to the OutputStream that you get back.

Alternatively, if you only need to run one command, look at the -c switch to su, which lets you run a command; or use "sudo", which is similar.

Now in practice, this is only going to work for userids that you can "su to" without a password, because if su reads a password, it does it by opening the raw terminal device, and you won't be able to intercept that from Java.


[Jess in Action][AskingGoodQuestions]
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
Why are the most obvious things also the hardest to get right. Thanks for all the help - I had misunderstood how the shell was accessed by java - all is good!

Cheers!

Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More Runtime questions