Sign in to follow this  
Followers 0
kjBulletkj

Communication with AutoIt and Java does not work

4 posts in this topic

Hi,

I am developing a test framework for automated tests. For this my Java Code generates AutoIt-scripts. Additionally there are some cases, where static scripts are needed, for example to execute several .bat-files.

Here starts my problem.

At first I am using Java to call an AutoIt-script, which executes a "server-bat". This works pretty well.

After this I try to do the same with a "client-bat", what does not work.

So here is my code for the server part, which works:

Java:

public static boolean startServer(String pathToServerFolder)
    {
        File f = new File(pathToServerFolder);
        boolean fExists = f.exists();
        
        System.out.println("Does directory exist? " + fExists);
        
        try {
            ProcessBuilder builder;
            
            List<String> commands = new ArrayList<String>();
            commands.add(Paths.PATH_TO_ROOT_DIR + Paths.PATH_TO_SERVER_STARTER_SCRIPT);
            
            System.out.println("Execute Server-Starter-Script in " + Paths.PATH_TO_ROOT_DIR + Paths.PATH_TO_SERVER_STARTER_SCRIPT + " \nwith parameter " + pathToServerFolder);
            commands.add("\"" + pathToServerFolder + "\"");
                
            builder = new ProcessBuilder(commands);
            builder.redirectErrorStream(true);
            Process process = builder.start();
            
            return true;
            
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
    }

and here is my really simple AutoIt-script for that part:

$path = $CmdLine[1]

FileChangeDir($path)
Run("start-server.bat")

That's all.

Following I will post the code, which does not work. I really don't know, why it doesn't work because it is really just copy&paste with some different values.

Java-Code of the not working client part:

public static boolean startClient(String pathToClientFolder)
    {
        try {
            ProcessBuilder builder;
            
            List<String> commands = new ArrayList<String>();
            commands.add(Paths.PATH_TO_ROOT_DIR + Paths.PATH_TO_CLIENT_STARTER_SCRIPT);
            System.out.println("Execute Client-Starter-Script in " + Paths.PATH_TO_ROOT_DIR + Paths.PATH_TO_CLIENT_STARTER_SCRIPT + " \n with parameter " + pathToClientFolder);
            commands.add(" \"" + pathToClientFolder + "\"");
                
            builder = new ProcessBuilder(commands);
            builder.redirectErrorStream(true);
            Process process = builder.start();
            
            return true;
            
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
    }

and the AutoIt-Code:

$path = $CmdLine[1]

FileChangeDir($path)
Run("start-small-console.bat")

I think, that the error occurs, when I am giving the path as a parameter, because an AutoIt-Error-message says: "Error:Subscript used on non-accessible variable." on Line 2. Something must be wrong with the path. But as you can see I defined some printlns which are outputting the path. If I copy the paths from the output, and paste them into the Windows Explorer, it opens the correct directory or the correct .bat-file. This confuses me really, because it is exactly the same code as above, in the server part. I don't know what's wrong with the path, and I don't know, what it could be else.

I hope you guys are able to help me.

Thank.

Share this post


Link to post
Share on other sites



 

That error is saying that the autoit client code is not being passed a path parameter.

MsgBox(0,"passed arguments", $CmdLine[0)]

$path = $CmdLine[1]

FileChangeDir($path)
Run("start-small-console.bat")

I know, but I do not get why, and I don't know, how to solve this. In the server part the code works. In the client part it doesn't. I try to pass a path as a parameter. If I execute my script with the cmd, with giving the same path as a parameter, it works. Pasting the path, which is used in the Java code, into the windows explorer directs me to the correct directory. As you can see, all my tries to do that by hand, what my code is supposed to do, were successful. Somewhere in or around the "commands.add()" must be wrong.

This is what my Eclipse console displays:

For the server part:

Execute Server-Starter-Script in D:testSWscriptsstartServer.exe

with parameter D:testSWtestworkingDirectoryFirstTestserver

Server should be started from D:testSWtestworkingDirectoryFirstTestserver

Again, the server gets started successfully from that location.

 

And now for the client part:

Going to start the client from location D:testSWtestworkingDirectoryFirstTestclient

Execute Client-Starter-Script in D:testSWscriptsstartClient.exe

 with parameter D:testSWtestworkingDirectoryFirstTestclient

Since the script gives me an error in Line 2 of itself, we can see, that the execution (or to be more precise: the access) of my client-starter-script ("startClient.exe") works.

But the path parameter can't be handled somehow. For me there the path parameter doesn't look wrong.

Share this post


Link to post
Share on other sites

Solved it.

The following line contains the error:

commands.add(" \"" + pathToClientFolder + "\"");

This lines holds the parameter for my console command.

That command in its full size should look like: startClient.exe "D:pathtoconsole.bat"

Unfortunately the upper line puts two whitespaces between the console command and its parameter, what makes it look like this: startClient.exe--"D:pathtoconsole.bat"

I replaced the whitespaces with a "-", so that you clearly can see, that there are two whitespaces. I don't know if the windows console has problems with multiple spaces between a command and its parameters or if it's just the Java ProcessBuilder, which can't work with that. At least the removal of the whitespace solves my problem.

The correct line should look like this:

commands.add("\"" + pathToClientFolder + "\"");

I checked my path parameters multiple times, and I checked my code on all other places. I was about to research the ProcessBuilder mechanics, when I stumbled over that line just wondering, why there is a whitespace. I removed it, just because it didn't supposed to be there, and not because I thought it caused the error. After a whole day of research and searching, for my surprise, I found my bug.

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  
Followers 0