简体   繁体   中英

Unable to launch cmd or notepad using Autoit in Java

Hello I am using AutoIt in my Java Program using autoitx4java.Below is the code I am using:

import java.io.File;

import autoitx4java.AutoItX;

import com.jacob.com.LibraryLoader;

public class MyTest {

    public static void main(String[] args) throws InterruptedException{
        // TODO Auto-generated method stub
        String jacobDllVersionToUse;
        if (jvmBitVersion().contains("32")){
        jacobDllVersionToUse = "jacob-1.18-x86.dll";
        }
        else {
        jacobDllVersionToUse = "jacob-1.18-x64.dll";
        }

        File file = new File("lib", jacobDllVersionToUse);
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());

        AutoItX x = new AutoItX();
     // System.out.println(file.getAbsolutePath());
        x.run("cmd.exe");


    }

    private static String jvmBitVersion() {
        // TODO Auto-generated method stub
        System.out.println(System.getProperty("sun.arch.data.model"));
        return System.getProperty("sun.arch.data.model");

    }

}

When i run this program nothing happens and there is no error also.This also happens if i replace cmd.exe with notepad.exe. However when i replace cmd.exe with calc.exe calculator launches.

I am new to AutoIt and using the below link to setup AutoIt with Java:

http://www.joecolantonio.com/2014/07/02/selenium-autoit-how-to-automate-non-browser-based-functionality/

This might be that run does not find cmd and notepad if its looking in the wrong path, could be a 32/64 bit issue. Or the process is started but window is not visible, so check if the process runs in task manager.

Run will not give an error, but it will return "0 and set @error to non-zero". If it succeeds, it will return "the PID of the process that was launched" https://www.autoitscript.com/autoit3/docs/functions/Run.htm

To see if it succeeds, try this and see if it returns a pid:

MsgBox(0, "test", run("cmd.exe"))

If it returns 0 it probably means that program was not found. Try full filepath, for example:

MsgBox(0, "test", run("cmd.exe", "C:\Windows\System32\"))

If a pid is returned and the process is running but you see no window, try to add @SW_SHOW flag:

run("cmd.exe", "", @SW_SHOW)

run(“ cmd.exe”,“”,@SW_SHOW)起作用了!

Hi to open the notepad you can use the below code. As this is working for me.

    AutoItX x = new AutoItX();
    x.run("notepad.exe","",AutoItX.SW_SHOW);
    x.winActivate("Untitled - Notepad");
    x.winWaitActive("Untitled - Notepad");
    x.send("This is some text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM