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

    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

    java.lang.UNIXProcess@79bbdc

  2. #2
    levent Guest

    Default

    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.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 &#37;s is:", 
              Arrays.toString(args));
    
           while ((line = br.readLine()) != null) {
             System.out.println(line);
           }
    
         }
        }
    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
    Posts
    8
    Rep Power
    0

Similar Threads

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