Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: Problem with exec() and outputStream

  1. #1
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Problem with exec() and outputStream

    Hello
    I am trying to plot a graph (say sinx) using gnuplot through a java program. For this I'm trying to pipe a command into the pgnuplot application.
    The code is working fine but the only problem is that, when the pgnuplot.exe runs, it displays the result window with the sin graph for 10-20ms and then closes automatically. I want the result window to stay till the time i press a key or give a command for it to close.

    the code is as follows:
    Java Code:
    import java.io.*;
     
    public class Runex2
    {
     
           public static void main(String args[]) {
     
                try {
                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec("C:\\Program Files\\gnuplot\\bin\\pgnuplot.exe");    
    		OutputStream opStream = proc.getOutputStream();
    		PrintWriter gp=new PrintWriter(new BufferedWriter(new OutputStreamWriter(opStream))); 
    		gp.println("plot sin(x)\n");   
    		gp.close(); 
    		int exitVal=proc.waitFor();
                    System.out.println("Exited with error code "+exitVal);
     
                } catch(Exception e) {
                    System.out.println(e.toString());
                    e.printStackTrace();
                }
            }
    }
    Any sort of help will be appreciated.
    PS this is my first post.
    Last edited by er.raj; 03-07-2012 at 06:24 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Problem with exec() and outputStream

    pgnuplot.exe runs, it displays the result window with the sin graph for 10-20ms and then closes automatically
    What happens when you run that command from the command line without the java program?
    How do you keep the display open then?

  3. #3
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with exec() and outputStream

    Quote Originally Posted by Norm View Post
    What happens when you run that command from the command line without the java program?
    How do you keep the display open then?
    Well pgnuplot has a command-line interface, and when I write a command such as "plot [-3.14:3.14] sin(x)" over there, it will open a new separate window with the required graph. It can be closed like any other GUI window.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Problem with exec() and outputStream

    What happens if you don't close the output stream?
    Pause the thread dor a few seconds before calling the close().
    Does that delay the plot window closing?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with exec() and outputStream

    Quote Originally Posted by Tolls View Post
    What happens if you don't close the output stream?
    When i comment the line "gp.close();" and run the program, pgnuplot opens with a blank screen and it hangs thereafter. No graph is generated even for a fraction of a second.

    Quote Originally Posted by Tolls View Post
    Pause the thread dor a few seconds before calling the close().
    Does that delay the plot window closing?
    Good point.
    I tried to place a "Thread.sleep(4000);" after Line 13 in my code i.e before the command "gp.close()".
    Result : It opens pgnuplot with a blank screen and then goes in a sleep state. After 4secs the graph is generated again for a fraction of a second and then everything terminates.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Problem with exec() and outputStream

    I should have also said "do a flush()".
    Java Code:
        OutputStream opStream = proc.getOutputStream();
        PrintWriter gp=new PrintWriter(new BufferedWriter(new OutputStreamWriter(opStream)));
        gp.println("plot sin(x)\n");
        gp.flush();
    // Sleep.
    // Eventually close
    Anyway, it looks like closing the stream tells the plot window it's not needed anymore.
    So keep the stream open until whatever event you want to close it happens?
    er.raj likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with exec() and outputStream

    Quote Originally Posted by Tolls View Post
    I should have also said "do a flush()".
    Wow...finally some relief to the eyes. The graph is showing up for 4 seconds. Thank you so much.

    But I still wonder if it will solve my problem in general. This is only a segment of a larger code, and I will be aiming to generate graphs in parallel.
    I will need the graphs to stay open for any time period.
    And when the graph window which is generated is in itself a normal GUI with "min max close" buttons, why cant we just leave it on the user to close the graph window when he desires to do so ? There must be a way to leave the application running without keeping any dependencies from the source, isnt it ?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Problem with exec() and outputStream

    I have no idea, but I suspect it is to do with child processes.
    When your output stream closes (or possibly the app as a whole? you might want to try sleeping after the close) it's closing the parent/child tie.

    That's a bit of a guess on my part, it has to be said.
    When run from the cmd line are you able to still use the cmd terminal?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with exec() and outputStream

    Quote Originally Posted by Tolls View Post
    When run from the cmd line are you able to still use the cmd terminal?
    That was an interesting question. I checked it out and yes I am able to use the pgnuplot command line interface when run from the cmd i.e on executing the java program.
    I am attaching a snapshot just for clarity. This is taken on running the java program (during the sleep time interval). Note: No additional command is written on the pgnuplot command line after execution.

    Problem with exec() and outputStream-snap1.jpg

Similar Threads

  1. exec problem, compiles but no output?
    By Dark in forum New To Java
    Replies: 21
    Last Post: 04-17-2011, 09:24 AM
  2. OutputStream.flush() problem
    By Godjikung in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:48 PM
  3. OutputStream.flush() problem
    By Godjikung in forum Advanced Java
    Replies: 3
    Last Post: 01-29-2011, 06:36 AM
  4. Problem with Runtime.getRuntime().exec with Linux Commands
    By swapnilnawale in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-23-2009, 11:23 PM
  5. Problem with Runtime.exec()
    By nhabibi in forum Advanced Java
    Replies: 11
    Last Post: 07-02-2008, 02:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •