Jump to content
Sign in to follow this  
EamonB

Passing Autoit exe return value into Java

Recommended Posts

EamonB

I am doing some simple tests in eclipse where I pass parameters from Java code into my AutoIt exe successfully.

But now I want to see if there is a way in Java to capture whether or not the AutoIt exe ran correctly (possibly by having the executable return a boolean that can be passed back to the Java code)

Here is my original sample code

String[] cmd = new String[3];

cmd[0] = AutoItScripts.compareDocsWord;
cmd[1] = "C:\\Users\\Eamon\\Desktop\\file1.doc"; 
cmd[2] = "C:\\Users\\Eamon\\Desktop\\file2.doc";
        
Runtime.getRuntime().exec(cmd);

Although I understand the code below obviously doesn't work, I was wondering if there was any sort of way to pass the result of the AutoIt exe back into Java in a way similar to this example

int x = 5;

String[] cmd = new String[3];
cmd[0] = AutoItScripts.compareDocsWord;
cmd[1] = "C:\\Users\\Eamon\\Desktop\\file1.doc"; 
cmd[2] = "C:\\Users\\Eamon\\Desktop\\file2.doc";
        
Boolean result = Runtime.getRuntime().exec(cmd);
if (result==false)
    x+=7;
Edited by EamonB

Share this post


Link to post
Share on other sites
Melba23

EamonB,

What he said. Threads merged. :naughty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
EamonB

My bad. I didn't know how to delete my previous post so I created a new one.
Also, for anyone else's reference, I figured out how to pass values from the executable back into Java.
AutoIt has a thing called Exit return values. Scripts normally return an exit value of 0 if the script executed properly but you can set the exit value to any number above 0 that you want depending on what condition you want to return to Java.

For example, this is my autoit script same code:

$var = 2
if $var > 4 Then
    Exit(1)
Else
    Exit(2)
EndIf

Since $var is less than 4, the script will have the exit value of 2

Here is my corresponding Java code that calls the Autoit exe:

String[] cmd = new String[1];
cmd[0] = AutoItScripts.test;
        
Process result = Runtime.getRuntime().exec(cmd);
result.waitFor();   
    
if (result.exitValue()==1)
    System.out.println("YAY!");
else
    System.out.println("boo");

You can expect the console to print out "boo" instead of "YAY!" because the exit value is not 1 (it's 2 because the value of $var was less than 4).

NOTE: keep in mind that you have to add the result.waitFor() function (waits for the exe to finish running) in your java code before you verify the exit value result because otherwise the java code is going to check the exit value before the executable even finishes running.

Hopefully that helps anyone else that had a similar problem to me!!

Edited by EamonB

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  

×