participate


Java Programming - Executing Linux command from Java
<<   Back to Forum  |   Give us Feedback
This topic has 3 replies on 1 page.
rajnishe
Posts:75
Registered: 12/6/04
Executing Linux command from Java   
Jun 3, 2006 3:28 AM

 
Hello all
I want to run linux command "ps | grep user" from java program and then writing output from this to a file . How can i do this ?

Thanks
 
sabre150
Posts:21,245
Registered: 10/24/97
Re: Executing Linux command from Java   
Jun 3, 2006 3:50 AM (reply 1 of 3)  (In reply to original post )

 
import java.io.*;
 
public class Fred401
{    
    public static void main(String[] args)
    {
        try
        {
            String[] command = {"sh","-c","ps -elf | grep sabre"};
            final Process process = Runtime.getRuntime().exec(command);
            new Thread()
            {
                public void run()
                {
                    try{
                        InputStream is = process.getInputStream();
                        byte[] buffer = new byte[1024];
                        for(int count = 0; (count = is.read(buffer)) >= 0;)
                        {
                            System.out.write(buffer, 0, count);
                        }
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }.start();
            new Thread()
            {
                public void run()
                {
                    try{
                        InputStream is = process.getErrorStream();
                        byte[] buffer = new byte[1024];
                        for(int count = 0; (count = is.read(buffer)) >= 0;)
                        {
                            System.err.write(buffer, 0, count);
                        }
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }.start();
            
            int returnCode = process.waitFor();
            System.out.println("Return code = " + returnCode);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
 
guardian07
Posts:11
Registered: 6/27/07
Re: Executing Linux command from Java   
Jul 2, 2007 5:08 PM (reply 2 of 3)  (In reply to #1 )

 
Hi sabre,

awesome chunk of code there.. I have used it in a fwe of my projects (I have of course given you credit in the about dialog, and in the comments of the code)

The thing is, I am working on somthign a bit more complex now, and I'm really stuck, so I was hoping you could help.

When I use this code, I always remove the line:
[String[] command = {"sh","-c","ps -elf | grep sabre"};

and have it get the command from the constructor....


What I need now though, is to be able to get your method to return the console result from the command... so I want to be able to do this:

Public void whatWineVersion(){
runLinuxCommand("wine --version"); //runLinuxCommand is your method
system.out.println(output from the ruinLinuxCommand output)
}


Normally I would be able to do this myself, but because your method uses nested threads, I can't figure out how to get the output!

Message was edited by:
guardian07
 
sabre150
Posts:21,245
Registered: 10/24/97
Re: Executing Linux command from Java   
Jul 3, 2007 12:22 AM (reply 3 of 3)  (In reply to #2 )

 
Just perform the read of stdout in the thread used to to execute the exec(). You don't need to use a separate thread for it; in my original I just used a separate thread to show how it could be done in a separate thread.

Make sure you start the stderr thread before you start reading from stdout.
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums