Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Write to a process once its started using getOutputStream()

Thread view: 
michael.shnitzer@gmail.com - 26 Jun 2005 22:22 GMT
I have a dos program that I wish to access through my java program... I
can run the program and read the programs output using

DataInputStream in = new DataInputStream(new BufferedInputStream
process.getInputStream()));

But when I try to write to the program using...

DataOutputStream out = new DataOutputStream(new
BufferedOutputStream(process.getOutputStream()));
out.write(command.getBytes());
out.flush();

I get no response from the program and it doesn't seem to respond to
the command.

I am trying this on xp.
Lee Fesperman - 27 Jun 2005 01:49 GMT
> I have a dos program that I wish to access through my java program... I
> can run the program and read the programs output using
[quoted text clipped - 13 lines]
>
> I am trying this on xp.

You didn't show the contents of command. Perhaps the program needs a newline.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

michael.shnitzer@gmail.com - 27 Jun 2005 03:18 GMT
It wasn't a new line problem... i tried new line and i have the flush
in there...

What is the correct way to use the getOutputStream of the process...
which is the stdin of the program opened by the process I assume.
Lee Fesperman - 27 Jun 2005 07:41 GMT
> It wasn't a new line problem... i tried new line and i have the flush
> in there...
>
> What is the correct way to use the getOutputStream of the process...
> which is the stdin of the program opened by the process I assume.

You need to do getOutputStream and getInputStream in separate threads. You may also need
to do getErrorStream (in yet another thread).

Have you tried writing a simple Java console application and calling that?

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

John Currier - 29 Jun 2005 04:45 GMT
> I get no response from the program and it doesn't seem to respond to
> the command.

What exactly do you mean by that statement?  Does your program block
when it does a process.waitFor()?

If there's a chance that the program you're executing will send
anything to stdout or stderr then you'll need to create a thread to
read from the stream(s) that it's writing to (process.getInputStream()
/ process.getErrorStream()) as Lee mentioned.  Note that if you don't
and if the program writes more data than the size of the output buffers
then the program will block waiting for someone to read from the other
side of those buffers...and nobody's there to read from it...deadlock.

John
http://schemaspy.sourceforge.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.