0

I am trying to figure out how to execute my AutoHotKey script from my Java program. The issue is I am not sure the best way to actually do this because when I do this I get an error...

Runtime runtime = Runtime.getRuntime();

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    try {
        String actualPath = "C:\\Users\\me\\Desktop\\Coding\\test.ahk";
        String autoHotKeyScript = "C:\\Program Files\\AutoHotkey";
        runtime.exec(new String[] {autoHotKey, actualPath});
    } catch (IOException e) {
        e.printStackTrace();
    }

With the error :

Cannot run program "C:\Program Files\AutoHotkey": CreateProcess error=5, Access is denied

  • Start by figuring out how you'd do something like this just from the commandline – MadProgrammer Sep 10 '21 at 01:42
  • Ok, some quick googling, you need to do something like `path_to/AutoHotKey.exe test.ahk`, but I'd investigate using `ProcessBuilder` instead of `Runtime.exec`, it will give you more control and options – MadProgrammer Sep 10 '21 at 01:45
  • Does this answer your question? [Java CreateProcess error=193, %1 is not a valid Win32 application](https://stackoverflow.com/questions/50156510/java-createprocess-error-193-1-is-not-a-valid-win32-application) – Gokul Nath KP Sep 10 '21 at 01:46
  • Gokul Nath KP that does not help me know it just tells me I have an issue which is nothing new. –  Sep 10 '21 at 01:48
  • @UnSure I'd recommend reading the [Java Docs for `Runtime.exec`](https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html). You should seperate the commands into different elements (of an array), where the first element is the command to execute and other elements are the parameters you want to pass – MadProgrammer Sep 10 '21 at 02:00
  • MadProgrammer yes you are right I have updated it since then but I am not getting an access denied error. I went into the security settings of AutoHotKey but everything has access to read and execute so I am not sure what the issue is. –  Sep 10 '21 at 02:02
  • 1
    @UnSure Also, what is the exact path to, and including, `AutoHotKey.exe`. I'd imagine it should be something like `C:/Program Files/AutoHotkey/AutoHotkey.exe` – MadProgrammer Sep 10 '21 at 02:08

1 Answers1

2
    Runtime runtime = Runtime.getRuntime();

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    try {
        runtime.exec("C:\\Program Files\\AutoHotkey.exe C:\\Users\\me\\Desktop\\Coding\\test.ahk");
    } catch (IOException e) {
        e.printStackTrace();
    }

add the AutoHotkeyU64.exe before your ahk script.

xwine
  • 91
  • 4