This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "More Runtime questions" Watch "More Runtime questions" New topic

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

Joined: Jul 08, 2003
Posts: 24183

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!


Consider Paul's rocket mass heater.
subject: More Runtime questions
Similar Threads
running unix 'su 'cmd from java
How to get both ErrorStream and InputStream of a Process
Runtime getRuntime() exec(String command) - How does this work?
Problem with su - user_name inside script
running native applications through web server