Results 1 to 3 of 3
Thread: get the output from whoami
- 06-11-2007, 11:33 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 8
- Rep Power
- 0
get the output from whoami
Hello, this is my first post so please be patient with me.
I am quite new to java and I am trying to get the output of a whoami
command into a String variable.
normally I use
Process p = Runtime.getRuntime().exec("whoami");
but with the whoami command all I get is
java.lang.UNIXProcess@79bbdc
- 06-12-2007, 12:26 PM #2levent Guest
Hello Gary,
If you have Java 5 and above you should use ProcessBuilder instead of Runtime.exec(). Here is an example program for your purpose:
You can run it with "java DoProcessBuilder whoami".Java Code:import java.io.*; import java.util.*; public class DoProcessBuilder { public static void main(String args[]) throws IOException { if (args.length <= 0) { System.err.println("Need command to run"); System.exit(-1); } Process process = new ProcessBuilder(args).start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; System.out.printf("Output of running %s is:", Arrays.toString(args)); while ((line = br.readLine()) != null) { System.out.println(line); } } }
Let us know if you have any other problems.
- 06-12-2007, 01:05 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Output Redirection
By Sixtease in forum New To JavaReplies: 8Last Post: 12-29-2008, 10:18 AM -
Writing an XML output
By JThangiah in forum XMLReplies: 2Last Post: 03-27-2008, 04:15 PM -
Output to excel
By abhiN in forum New To JavaReplies: 2Last Post: 03-07-2008, 01:19 AM -
output
By Camden in forum New To JavaReplies: 3Last Post: 12-01-2007, 10:34 PM -
How to redirect the output
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks