Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > exec() and sending to STDIO and reading from STDIO

Reply
Thread Tools

exec() and sending to STDIO and reading from STDIO

 
 
WinstonSmith_101@hotmail.com
Guest
Posts: n/a
 
      10-17-2006
I have an external program I need to execute which takes an argument.
So I have the code:

String command = "...";
String argument = "...";
process = Runtime.getRuntime().exec(command);

And then for sending arguments:

java.io.OutputStream out = process.getOutputStream();
out.write(argument.getBytes());
out.close();


And the for reading output from the external command:

java.io.InputStream in = encryptProcess.getInputStream();
int c;
while ((c = in.read()) != -1)
{
System.out.print("OUT: " + (char)c);
}
in.close();

= = = = = = = = = = = = = = = = = =

But the problem is that the output is not actually read. It's still
being outputted when I close the InputStream (in.close()

Is there not a way to but execute an external program, send arguments
to it and get it's output?


I have tried to insert the command "echo argument|command" e.g.
"echo test|ls" in the command, but this merely echos out the whole
line ("test|ls") rather than sending the argument "test" to
"ls".


Thanks
/Rune

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-17-2006
On 17 Oct 2006 09:44:37 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have tried to insert the command "echo argument|command" e.g.
> "echo test|ls" in the command, but this merely echos out the whole
> line ("test|ls") rather than sending the argument "test" to
> "ls".


It's hardly meaningful to send input to ls that way, but it fails
because you are attempting to use shell features where there is no
shell. You need to do it like this:

String[] cmd = { "/bin/sh", "-c", "echo foo | gurka" };
Process p = System.getRuntime().exec(cmd);

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
WinstonSmith_101@hotmail.com
Guest
Posts: n/a
 
      10-18-2006

Gordon Beaton wrote:
> On 17 Oct 2006 09:44:37 -0700, (E-Mail Removed) wrote:
> > I have tried to insert the command "echo argument|command" e.g.
> > "echo test|ls" in the command, but this merely echos out the whole
> > line ("test|ls") rather than sending the argument "test" to
> > "ls".

>
> It's hardly meaningful to send input to ls that way, but it fails
> because you are attempting to use shell features where there is no
> shell. You need to do it like this:
>
> String[] cmd = { "/bin/sh", "-c", "echo foo | gurka" };
> Process p = System.getRuntime().exec(cmd);


Hello thanks for the feedback, but this merely echos out "foo | gurka".

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-18-2006
On 18 Oct 2006 00:46:36 -0700, (E-Mail Removed) wrote:
> Hello thanks for the feedback, but this merely echos out "foo | gurka".


No, it doesn't. Post your *actual* code. You are not running a shell
as I suggested.

This is what happens when I run it with real commands:

bash$ cat Test.java
import java.io.*;

public class Test {
public static void main(String[] args) throws Exception {
String[] cmd = { "/bin/sh", "-c", "echo this is a test | wc" };
Process p = Runtime.getRuntime().exec(cmd);

BufferedReader br = new BufferedReader(new InputStreamReader (p.getInputStream()));

String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}

base$ javac Test.java
bash$ java Test
1 4 15
bash$

The example is complete and compilable. Try it.

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
WinstonSmith_101@hotmail.com
Guest
Posts: n/a
 
      10-18-2006

Gordon Beaton wrote:
> On 18 Oct 2006 00:46:36 -0700, (E-Mail Removed) wrote:
> > Hello thanks for the feedback, but this merely echos out "foo | gurka".

>
> No, it doesn't. Post your *actual* code. You are not running a shell
> as I suggested.
>
> This is what happens when I run it with real commands:
>
> bash$ cat Test.java
> import java.io.*;
>
> public class Test {
> public static void main(String[] args) throws Exception {
> String[] cmd = { "/bin/sh", "-c", "echo this is a test | wc" };
> Process p = Runtime.getRuntime().exec(cmd);
>
> BufferedReader br = new BufferedReader(new InputStreamReader (p.getInputStream()));
>
> String line;
> while ((line = br.readLine()) != null) {
> System.out.println(line);
> }
> br.close();
> }
> }
>
> base$ javac Test.java
> bash$ java Test
> 1 4 15
> bash$
>
> The example is complete and compilable. Try it.


- your example works. But when I substitude the program gpg for wc, I
stop getting the output. Can it be that it sends on STDERR or
something?

import java.io.*;



public class Test1
{
public static void main(String[] args) throws Exception
{
String[] cmd = { "/bin/sh", "-c", "echo mypasswd | gpg
--passphrase-fd 0 -o tobe.zip -q --decrypt tobe.zip.gpg" };



Process p = Runtime.getRuntime().exec(cmd);



BufferedReader br = new BufferedReader(new InputStreamReader
(p.getInputStream()));



String line;
while ((line = br.readLine()) != null)
{
System.out.println("XXX: " + line);
}



br.close();
}
}

- it's the same. I just changed the "String[] cmd =" line

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-18-2006
On 18 Oct 2006 04:30:31 -0700, (E-Mail Removed) wrote:
> But when I substitude the program gpg for wc, I stop getting the
> output. Can it be that it sends on STDERR or something?


It's quite possible that gpg is sending output to stderr, but you
should be reading from both streams anyway (which my simple example
doesn't do). To do this correctly you need to read from the error
stream from a separate thread.

An alternative is to to use ProcessBuilder, which can combine both
streams into one and you don't need any extra thread. But since you're
using a shell anyway, you can make a simple change to the command and
it will combine the output streams for you:

String[] cmd = {
"/bin/sh",
"-c",
"echo mypasswd | gpg --passphrase-fd 0 -o tobe.zip -q --decrypt tobe.zip.gpg 2>&1"
};

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
aeonsun
Guest
Posts: n/a
 
      10-20-2006
how can i reading from stdio use this command:
type mytest.txt (win32)

use String[] cmd = {"cmd","/c","type mytest.txt"} ,it dosn't work.

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-20-2006
On 20 Oct 2006 00:45:15 -0700, aeonsun wrote:
> how can i reading from stdio use this command:
> type mytest.txt (win32)
>
> use String[] cmd = {"cmd","/c","type mytest.txt"} ,it dosn't work.


You don't need cmd.exe for this, other than that I have no idea.

(in fact you don't need Runtime.exec() for this at all).

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      10-20-2006
Gordon Beaton wrote:
> On 20 Oct 2006 00:45:15 -0700, aeonsun wrote:
>> how can i reading from stdio use this command:
>> type mytest.txt (win32)
>>
>> use String[] cmd = {"cmd","/c","type mytest.txt"} ,it dosn't work.

>
> You don't need cmd.exe for this, other than that I have no idea.


Since type is an internal command in cmd and not a type.exe, then
I would think cmd is needed.

Arne
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      10-20-2006
aeonsun wrote:
> how can i reading from stdio use this command:
> type mytest.txt (win32)
>
> use String[] cmd = {"cmd","/c","type mytest.txt"} ,it dosn't work.


Try:

String[] cmd = {"cmd","/c", "type", "mytest.txt"};

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#include <cstdio> and #include <stdio.h> in the same file!? james.d.mclaughlin@gmail.com C++ 2 01-23-2013 06:00 PM
problem in visual stdio 2005 with bison and flex eng.sharif@gmail.com C++ 2 03-02-2007 12:21 AM
problem in visual stdio 2005 with bison and flex eng.sharif@gmail.com C++ 0 02-27-2007 01:17 AM
difference between stdio.h and cstdio david wolf C++ 2 12-06-2006 08:30 PM
SGI CC compiler: What happens if I use --> #include "unistd.h" and "stdio.h"? clusardi2k@aol.com C++ 2 09-07-2005 05:31 PM



Advertisments