Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2007, 12:33 PM
Member
 
Join Date: Jun 2007
Posts: 8
gary is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-12-2007, 01:26 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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:

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); } } }
You can run it with "java DoProcessBuilder whoami".

Let us know if you have any other problems.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-12-2007, 02:05 PM
Member
 
Join Date: Jun 2007
Posts: 8
gary is on a distinguished road
Thank you very much.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing an XML output JThangiah XML 2 03-27-2008 05:15 PM
Output Redirection Sixtease New To Java 2 03-22-2008 10:59 AM
Output to excel abhiN New To Java 2 03-07-2008 02:19 AM
output Camden New To Java 3 12-01-2007 11:34 PM
How to redirect the output JavaBean Java Tips 0 10-04-2007 10:30 PM


All times are GMT +3. The time now is 03:25 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org