communicating with process
hi,
i`m trying to launch a python script, send some strings to it and then receive back the strings, it puts out. right now i start a command line with "cmd" and receive the following: Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
so this is working ok. the code looks like this:
try{
String cmd ="cmd";
String line =null;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader lsOut =new BufferedReader(new InputStreamReader(p.getInputStream() ) );
while( ( line2=lsOut.readLine() ) !=null){
System.out.println(line);
}
}catch (Exception e){
System.err.println("ls error " +e);
}
if i put in p instead of line at the out.println, i get this:
java.lang.ProcessImpl@c17164
java.lang.ProcessImpl@c17164
java.lang.ProcessImpl@c17164
(seems to be the number of the process)
now i want to send a string (tagger.py) to the opened cmd, which opens a tagger program and then send text to it, which it tags and gives back. i`m not really sure of how to communicate with the process in order to do that. i found out that it is possible with getOutputStream. i have tried around with it, but i just can`t get it to work. does anyone know how exactly to use this method or is there maybe another method, that can be done with?
thx,
james
> ad of line at the out.println, i get this:
> java.lang.ProcessImpl@c17164
> java.lang.ProcessImpl@c17164
> java.lang.ProcessImpl@c17164
>
> (seems to be the number of the process)
Oh boy.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#toString()
How did you try to work with those streams? Show the code please.
maybe i should mention that i started programming java about a week ago. before i was completely on python, perl and c.
what i tried basically looked something like this. after the cmd opened i took the process "p" and wanted to pass the string. the line with the getoutputstream i saw on the net and tried to work with it, but i could not put in the string i want to pass and also i am not sure how to deal with PrintWriter. that was why i didn`t post these code lines. i don`t think they are worth anything. but here they are:
PrintWriter outPut;
try {
//String cmd = "cmd";
String line2 = null;
Process p = Runtime.getRuntime().exec("cmd");
outPut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(p.getOutputStream() ) ) );
//DataInputStream is = new DataInputStream(sock.getInputStream());
BufferedReader lsOut = new BufferedReader(new InputStreamReader(p.getInputStream() ) );
while( ( line2=lsOut.readLine() ) != null) {
System.out.println(outPut);
}
} catch (Exception e) {
System.err.println("ls error " +e);
}
what exactly did you mean with the hint for toString? when i run p through toString, i still get the same output
come on, is there nobody who knows how to work with getOutputStream? i can't believe this!!!! it can't be that hard
Don't get antsy with me mate - it really really won't do you any good around here.
System.out.println(line2);
What you are trying to do is actually quite difficult. However there is a simpler solution. Programatically create a batch file with what you need in it. Then use Runtime to launch it.
You'll find good info on creating files here: -
http://www.exampledepot.com/egs/java.io/WriteToFile.html
> come on, is there nobody who knows how to work with> getOutputStream? i can't believe this!!!! it can't> be that hardLOL.Google this: 978-0671027032The first hit will be an book you can get at amazon.com. I recommend you buy it and read it.
?know how to create and write into a file :) so this
http://www.exampledepot.com/egs/java.io/WriteToFile.html
is not what i need
if what i wrote is too complicated, there could be another solution, which would be slower, but still acceptable. if it would be possible to start the python script directly (instead of cmd) and give the string to tag right with it as an argument. as there a possibility to do that in a quick and easy way.
by the way.....i'm not a beginner to programming (some people in this thread [petes1234 ] seem to thik that). i`ve been programming on a professional level for seven years. but never with java, mainly with perl and python and with c in the beginning
I don't know python, pearl, ....
but, to send new "commands" to your cmd proc.
OutputStream out = p.getOuputStream();
out.write("your command".getBytes());
Then you can read Error and Normal InputStreams to see the output of your command.
Hope I helped.
> by the way.....i'm not a beginner to programming
> (some people in this thread [petes1234 ] seem to thik
> that). i`ve been programming on a professional level
> for seven years. but never with java, mainly with
> perl and python and with c in the beginning
Oh, I did not impune your programming skills, far from it.
> ?know how to create and write into a file :) so this
>
> http://www.exampledepot.com/egs/java.io/WriteToFile.ht
> ml
> is not what i need
I don't think it's a question of knowing whether to write to a file or not; it just may be easier for python and java to communicate through a file... unless you're talking about jython.
> ?know how to create and write into a file :) so this
>
> http://www.exampledepot.com/egs/java.io/WriteToFile.ht
> ml
> is not what i need
>
> if what i wrote is too complicated, there could be
> another solution, which would be slower, but still
> acceptable. if it would be possible to start the
> python script directly (instead of cmd) and give the
> string to tag right with it as an argument. as there
> a possibility to do that in a quick and easy way.
>
> by the way.....i'm not a beginner to programming
> (some people in this thread [petes1234 ] seem to thik
> that). i`ve been programming on a professional level
> for seven years. but never with java, mainly with
> perl and python and with c in the beginning
You completely missed my point. Two solutions have been given to you - think about them. I couldn't care less how long you've been programming but you did specifically mention that you were new to Java; manners is lost on you.