Jump to content
Sign in to follow this  
roundar

Running script from java, can't understand ConsoleRead results

Recommended Posts

roundar

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] : )

Share this post


Link to post
Share on other sites
jackraymund

out.write is creating a new line, in au3 u only add a string

its like this

(0) to autoit | out.newLine();

to java

so where is problem

Edited by jackraymund

thanks you BrewManNH

Share this post


Link to post
Share on other sites
roundar

Edited OP, perhaps a bit more clear.


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

Share this post


Link to post
Share on other sites
Tankbuster

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?

Share this post


Link to post
Share on other sites
DigitalFacade82

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 :)
 

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Danyfirex
      By Danyfirex
      Hello guys.  It's been awhile since I shared something. So today I want to share my last project.
      What's Loga?
      Loga is a simple logging library to keep track of code with an integrated console.
      Features.
      Common log levels. Integrated console. Multiple instances. Custom color and font for each instance log level. Define output format with macros. Conditional and occasional Logging. Easy to use. Basic Usage:
      #include "..\Loga.au3" ;This are some of the default settings: ;Default log level $LOGA_LEVEL_TRACE ;output format: {Symbol}{LogIndex} {LevelName} {LongDateTime} {Message} ;Log to File is enabled. ;Log file name format: YYYYMMDDHHMM-Loga-InstanceIndex.log ;Custom Console is disabled by default. ;By default log to STDOUT. _LogaTrace("I'm Trace") _LogaDebug("I'm Debug") _LogaInfo("I'm Info") _LogaWarn("I'm Warn") _LogaError("I'm Error") _LogaFatal("I'm Fatal")  
      More examples here.
       
      Check Loga on GitHub.
       
      Loga Latest Release v1.0.1.
       
      Saludos
       
       
       
       
    • Raj0813
      By Raj0813
      Launch the web application which has security certificate associated. On launching we get "Select a Certificate from the list of certificates" . how to handle this certificates using AutoIT and certificates are in a drop down. i have only chrome browser to launch the application.  do i need to install autoIt or just the add jar files and dll to it. it is not a pop up.
      Tried adding the certificate to trusted source dint work.. 
      Please find the screenshot attached for the screen obtained.

    • boomingranny
      By boomingranny
      Use this UDF to add a console gui to your script (with log file):
      It uses the Hidden Autoit window (that you probably didn't even know existed)
      Closing Console window will terminate script.
      example of console:

      #include-once #include <GuiEdit.au3> EnableConsoleGui("example.log") ;example: ;------------------------ ConsoleWrite ("Hello World") For $i = 1 To 10 ConsoleWrite (".") Sleep(200) Next ConsoleWrite ("done") ConsoleWrite(@CRLF) ConsoleWrite ("close me to exit"&@CRLF) While 1 Sleep(1000) WEnd ;------------------------ ;end of example code Func EnableConsoleGui($Logfile="") ;EnableConsoleGUI ;by Daniel Barnes 20/04/2018 ;Uses AutoIt's Hidden window as a console (output only) Global $pidChild ;if we don't have a parent (as the parent window would have our script name) If Not WinExists(StringTrimRight(@ScriptName,4)) Then Opt("TrayIconHide",1) ;get Autoit's hidden window handle local $hWnd = WinGetHandle(AutoItWinGetTitle()) ;move the autoit hidden window to the middle of the screen WinMove($hWnd, "", (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ;get the Handle of the edit box in Autoit's hidden window $hEditBox = ControlGetHandle($hWnd,"","[CLASS:Edit; INSTANCE:1]") ;show it WinSetState($hWnd, "", @SW_SHOW) ;set its title = our script name WinSetTitle($hWnd,"",StringTrimRight(@ScriptName,4)) ;Spawn a child "copy" of the script, enabling reading of its console... If @Compiled Then ;2 = $STDOUT_CHILD. This avoids requiring the AutoItConstants.au3 in this sample code $pidChild= Run( FileGetShortName(@ScriptFullPath),@ScriptDir,"",2) Else ;2 = $STDOUT_CHILD. This avoids requiring the AutoItConstants.au3 in this sample code $pidChild= Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath),@ScriptDir,"",2) EndIf OnAutoItExitRegister("EnableConsoleGui_CloseChildPID") ;read the console, while the child window exists (and the console window is visible) While ProcessExists($pidChild) $ConsoleRead = StdoutRead($pidChild) If $ConsoleRead then $text = StringLeft(ControlGetText($hWnd,"",$hEditBox),65535) If $Logfile Then FileWrite($Logfile,$ConsoleRead) $text &= $ConsoleRead ControlSetText($hWnd,"",$hEditBox,$text) ConsoleWrite($ConsoleRead) ;scroll to bottom of console edit window _GUICtrlEdit_SetSel($hEditBox, 65535, 65535) endif Sleep(250) WEnd exit endif EndFunc Func EnableConsoleGui_CloseChildPID() ;if this func isn't used ;when you close the console gui ;the child "clone" of your script will keep running ProcessClose($pidChild) EndFunc
    • XaelloNegative
      By XaelloNegative
      Hi team,
      I have a "well-built" autoIt exe app. Its running smooth until I open another application that when I run through the window info tool, returns as "SunAwtFrame". After running said application, my "well-built" autoIt exe app ceases to run.
      What seems to be the problem here and is there a possible turn around for it? Any suggestion or recommendation will be greatly appreciated. Thanks in advance.
      ~XN~
    • ags911
      By ags911
      I have a script that has to work on multiple resolutions but each resolution has slightly different co-ordinates due to automatic UI scaling. I have had to make separate files for each but would like to implement them all in one script. I have a similar program written for Java which uses else if statements to use different co-ordinates for each resolution after it has been detected. I'm not good with Java so I would like to implement this on AutoIt before later making a Java version.
       
      This is a snippet of the autoit code I have.
      ToolTip("1 - Search")
      MouseClick("Left", @DesktopWidth *0.823, @DesktopHeight *0.925, 1, 25)
      ToolTip("2 - Buy Now")
      MouseClick("Left", @DesktopWidth *0.83, @DesktopHeight *0.798, 1, 27)
      ToolTip("3 - OK")
      MouseClick("Left", @DesktopWidth *0.555, @DesktopHeight *0.596, 1, 15)
      ToolTip("4 - OK Clear Error")
      MouseClick("Left", @DesktopWidth *0.49, @DesktopHeight *0.597, 1, 30)
      ToolTip("5 - Back to Search")
      MouseClick("Left", @DesktopWidth *0.161, @DesktopHeight *0.108, 1, 15)
       
      This is a snippet of a java code I used.
      Thanks.
      private static void goToSearch(double maxX, double maxY, Robot bot) throws InterruptedException {     int currentX = 0;     int currentY = 0;     if (maxX == 2650 && maxY == 1440) {         currentX = 734;         currentY = 1316;     } else if (maxX == 1920 && maxY == 1200) {         currentX = 551;         currentY = 1096;     } else if (maxX == 1920 && maxY == 1080) {         currentX = 551;         currentY = 1042;     } else if (maxX == 1680 && maxY == 1050) {         currentX = 482;         currentY = 959;     } else if (maxX == 1440 && maxY == 900) {         currentX = 413;         currentY = 822;     } else if (maxX == 1366 && maxY == 768) {         currentX = 392;         currentY = 741;     } else if (maxX == 1280 && maxY == 800) {         currentX = 367;         currentY = 731;
×