Results 1 to 3 of 3
  1. #1
    gary is offline Member
    Join Date
    Jun 2007
    Rep Power

    Default 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


  2. #2
    levent 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:

    Java Code:
       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");
           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 &#37;s is:", 
           while ((line = br.readLine()) != null) {
    You can run it with "java DoProcessBuilder whoami".

    Let us know if you have any other problems.

  3. #3
    gary is offline Member
    Join Date
    Jun 2007
    Rep Power

Similar Threads

  1. Output Redirection
    By Sixtease in forum New To Java
    Replies: 8
    Last Post: 12-29-2008, 11:18 AM
  2. Writing an XML output
    By JThangiah in forum XML
    Replies: 2
    Last Post: 03-27-2008, 05:15 PM
  3. Output to excel
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 02:19 AM
  4. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 11:34 PM
  5. How to redirect the output
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts