Jump to content

Running script from java, can't understand ConsoleRead results


Recommended Posts

I'm trying to write to the output stream and read the input stream of a simple Autoit script from java. If I do not use the newLine() character, I get the expected output: a line is sent to auto it, a line is sent to java, and that is repeated. If I add the newLine() character, it seems every cycle an extra line is sent to autoit. Why would this be?

Java:

p = Runtime.getRuntime().exec("Test");

in = new BufferedReader( new InputStreamReader(p.getInputStream()));
out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()));
int i=0;

out.write("(" + i++ + ") to autoit");
out.newLine();
out.flush();

while ((line = in.readLine()) != null) {

System.out.println(line);

out.write("(" + i + ") to autoit");
out.newLine();
out.flush();

if(i++ > 9)
     p.destroy();
}

Autoit:

Local $line

While (True)

$line = ConsoleRead()

ConsoleWrite( $line & "to java" & @LF )

Sleep(25)

WEnd

Output:

(0) to autoit
to java
(1) to autoit
(2) to autoit
to java
(3) to autoit
(4) to autoit
(5) to autoit
to java
(6) to autoit
(7) to autoit
(8) to autoit
(9) to autoit
to java

Output I Expected:

(0) to autoit
to java
(1) to autoit
to java
(2) to autoit
to java
(3) to autoit
to java
(4) to autoit
to java
(5) to autoit
to java
(6) to autoit
to java
(7) to autoit
to java
(8) to autoit
to java
(9) to autoit
to java

Seems like ConsoleRead() doesn't acknowledge cr or lf as EOF.

Edited by roundar

: ([font=courier new,courier,monospace] EndFunc[/font] : )

Link to comment
Share on other sites

as far as I understood your code (java) you write 9 lines to the STDOUT of the bufferwriter out.

The Autoit it self loops with a sleep of 25ms.

You read than in a while the line (readline)

In my opinion the autoit writes constantly to the console the string: $line & "to java" &@LF

#AutoIt3Wrapper_Change2CUI=Y
#AutoIt3Wrapper_OutFile=exec_autoit.exe
Local $line

While (True)

$line = ConsoleRead()
if @extended>0 Then
ConsoleWrite( $line & "to java - bytes :"&@extended &@LF )
EndIf

Sleep(25)

WEnd

as I'm not much of a java coder here is my au3 of your java code.

#include
#include
#AutoIt3Wrapper_Change2CUI=Y
_DebugSetup("Debug", True,2)
Local $foo = Run("exec_autoit.exe", @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Local $line
for $i = 1 to 9
StdinWrite($foo,"("&$i&") to autoit")
$line = StdoutRead($foo)
If @error Then ExitLoop
_Debugout( $line)
sleep(100)
next

ProcessClose($foo)
_Debugout("Exit")

When you run my example WITHOUT the "if @extend" line (remark them) - the output looks like this:

D:\1work>STD_IN_READER.exe

AutoIt:3.3.8.1 Compiled (Os:WIN_7/SP1/X64 OSLang:0407)

(1) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

(2) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

(3) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

(4) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

to java - bytes :0

(5) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

(6) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

(7) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

to java - bytes :0

(8) to autoitto java - bytes :13

to java - bytes :0

to java - bytes :0

Exit

and with the if check

D:\1work>STD_IN_READER.exe

AutoIt:3.3.8.1 Compiled (Os:WIN_7/SP1/X64 OSLang:0407)

(1) to autoitto java - bytes :13

(2) to autoitto java - bytes :13

(3) to autoitto java - bytes :13

(4) to autoitto java - bytes :13

(5) to autoitto java - bytes :13

(6) to autoitto java - bytes :13

(7) to autoitto java - bytes :13

(8) to autoitto java - bytes :13

Exit

as always ... au3 fixes the java things :-)

Hint: If you play with your code and the sleep statement in au3 you will get wore results if you add "sleep(1)" as an example. Or better if you eg. add sleep(1000)

Do you agree with the answer?

Link to comment
Share on other sites

  • 4 months later...

Hey Tankbuster,

I know I am Nectro-bumping this thread. But I just wanted to say thanks your post was very valuable for me working out a way to eliminate standard Windows wrapper scripts to handle arguments to jar files in an app I am building. So thanks!

The OP never responded but I felt the need to. This forum is fantastic, so many intelligent people on here :)
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...