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 12 of 12

Thread: Why runtime error?

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Why runtime error?

    Hello.
    I get a run-time error executing my code. Can anyone tell me why?

    Thanks in advance

    Here is my class with main():
    class ExecDemo{
    	   // Date	d = new Date();
     
    	   public static void main( String args[] ){
    	   Runtime r = Runtime.getRuntime();
    	   Process p = null;
     
    	    try{
    		   p = r.exec("ls");
    		   p.waitFor();
    	       p=r.exec("ping 8.8.8.8");
    	   }
    	   catch(Exception e){
    		   System.out.println("Error executing ps");
    	   }//end try..catch block
    	   System.out.println("ps returned " + p.exitValue());	
    	   System.out.println("Total Memory is: " + r.totalMemory());
    	   System.out.println("Total Memory is: " + System.getProperty("CPU"));
     
    	}//end main( string args[] )
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why runtime error?

    Can you tell us what the error is?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Why runtime error?

    This is my screen output:

    soheil@soheil-desktop:~/test_suite$ java ExecDemo
    Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited
    at java.lang.UNIXProcess.exitValue(UNIXProcess.java:1 88)
    at ExecDemo.main(ExecDemo.java:25)
    soheil@soheil-desktop:~/test_suite$

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why runtime error?

    That error tells you that the Process hasn't exited. Your code waits for the first process to exit, but not the second one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    hooshdar3 (March 12th, 2014)

  6. #5
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Why runtime error?

    I called waitFor() for the 2nd instance of the process, but nothing appeared at the output.
    Similar;y, I wrote:
    p = r.exec("top -n1");
    p.waitFor();
    again, with no result whatsoever.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why runtime error?

    What output are you expecting?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Why runtime error?

    Quote Originally Posted by KevinWorkman View Post
    What output are you expecting?
    I expect the same output as when exdecuting free-m or top -n1 from the command prompt/terminal

    --- Update ---

    I look for a call similar to C++ system(), most efficient.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why runtime error?

    I would use a ProcessBuilder if I were you.

    And you need to separate your arguments. Instead of exec("top -n1") it would be exec("top", "-n1");
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Why runtime error?

    exec("top", "-n1"); errors at compile time

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why runtime error?

    Again, what is the error? Have you tried using a ProcessBuilder?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Why runtime error?

    This is the only info I get:

    ExecDemo.java:20: cannot find symbol
    symbol : method exec(java.lang.String,java.lang.String)
    location: class java.lang.Runtime
    p = r.exec("top" , "-n1");
    ^
    1 error
    soheil@soheil-desktop:~/test_suite$

    --- Update ---

    I need to obtain the memory status + CPU speed

  13. #12
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Why runtime error?

    The error message in your last post is simply stating that the Runtime class doesn't have an exec() method with 2 String parameters. See Runtime (Java Platform SE 7 ) for the different variants of the exec() method.

    Alternatively, as KevinWorkman suggested, you can use ProcessBuilder to do this. See ProcessBuilder (Java Platform SE 7 ).

Similar Threads

  1. Runtime array programm but there is error in it.
    By brishi2806 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 3rd, 2014, 11:49 AM
  2. Runtime error-- Applet program
    By amar5445 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2013, 07:32 AM
  3. Runtime Error
    By Dennis Enya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2013, 12:44 PM
  4. stack project-runtime error
    By makonikor in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 31st, 2011, 02:16 PM
  5. Runtime Error
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 10th, 2011, 04:09 PM