Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Invoke a linux shell command using Process

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Invoke a linux shell command using Process

    Hi every body,

    I am trying to execute this commad (/usr/bin/rrdtool lastupdate my_rrdfile_path) in java by Runtime.getRuntime.exe().
    executing this command in linux shell will return some status of my system. but when I execute it by java, it returns null!
    here is some part of my program:

     
                    String rrdCmd = "/usr/bin/rrdtool lastupdate " + rrdPath;
                    String temp = null;
    		String cmdStResult = null;			
    		Process p = null;
     
    		try {
    			p = Runtime.getRuntime().exec(rrdCmd);
    			int exitVal = p.waitFor();
    			System.out.println("exitVal of rrdLastUpdate is : " + exitVal);
     
    		     } catch (Exception e) {
     
    			System.out.println("Problem in exec()");
    			e.printStackTrace();
     
    			}//end of try-catch
     
     
     
    		try {				
     
    				InputStreamReader inStreamReader = new InputStreamReader(p.getInputStream());
    				BufferedReader stdInput = new BufferedReader(inStreamReader);
     
    				while ((s = stdInput.readLine()) != null) {
    					System.out.println(s);					
    				}//end of while
     
                          } catch (Exception ex) {
    				System.out.println("problem!");
    				ex.printStackTrace();
    		      }//end of try-catch


    I thought this forum is most related to my question!
    sorry if my english is poor
    any guidance would be appreciated
    thanks a lot.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Invoke a linux shell command using Process


  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Invoke a linux shell command using Process


  4. The Following User Says Thank You to copeg For This Useful Post:

    masoumeh (June 27th, 2013)

  5. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Invoke a linux shell command using Process

    sorry, I did not know these rules. thanks for your mention.
    excuse me. because I should solve this problem as quickly as possible and nobody answered me here, I asked it in another forum. but actually I didn't know this isn't right.
    every body should forgive me

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Invoke a linux shell command using Process

    can I ask my question any forum else by linking to here?
    now what can I do, when no answer is available

Similar Threads

  1. Invoke methods
    By wdh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2012, 08:17 AM
  2. Java Child process cannot execute properly for telnet command
    By mamunbu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 25th, 2011, 07:43 AM
  3. Replies: 1
    Last Post: June 7th, 2011, 11:53 AM
  4. how to invoke a method?????
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: December 2nd, 2010, 11:48 PM
  5. How to set java compiler compliance with command under Linux
    By Eric97 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 9th, 2010, 09:25 AM