Results 1 to 9 of 9
- 03-07-2012, 10:42 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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:
Any sort of help will be appreciated.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(); } } }
PS this is my first post.Last edited by er.raj; 03-07-2012 at 05:24 PM.
- 03-07-2012, 01:34 PM #2
Re: Problem with exec() and outputStream
What happens when you run that command from the command line without the java program?pgnuplot.exe runs, it displays the result window with the sin graph for 10-20ms and then closes automatically
How do you keep the display open then?
- 03-07-2012, 05:07 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
- 03-07-2012, 05:21 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 03-07-2012, 05:35 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Problem with exec() and outputStream
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.
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.
- 03-07-2012, 05:59 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Problem with exec() and outputStream
I should have also said "do a flush()".
Anyway, it looks like closing the stream tells the plot window it's not needed anymore.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
So keep the stream open until whatever event you want to close it happens?Please do not ask for code as refusal often offends.
- 03-07-2012, 06:14 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Problem with exec() and outputStream
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 ?
- 03-07-2012, 06:25 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 03-07-2012, 06:34 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Problem with exec() and outputStream
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.
Similar Threads
-
exec problem, compiles but no output?
By Dark in forum New To JavaReplies: 21Last Post: 04-17-2011, 08:24 AM -
OutputStream.flush() problem
By Godjikung in forum Advanced JavaReplies: 2Last Post: 01-29-2011, 07:48 PM -
OutputStream.flush() problem
By Godjikung in forum Advanced JavaReplies: 3Last Post: 01-29-2011, 05:36 AM -
Problem with Runtime.getRuntime().exec with Linux Commands
By swapnilnawale in forum Threads and SynchronizationReplies: 1Last Post: 09-23-2009, 10:23 PM -
Problem with Runtime.exec()
By nhabibi in forum Advanced JavaReplies: 11Last Post: 07-02-2008, 01:35 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks