2016-04-13 35 views
2

在這裏,我試圖用Java在AutoIt中打開記事本。但是我無法用下面的代碼打開它。無法在AutoIt中用Java打開記事本

public class Notepad { 
    public static String jvmBitVersion() { //returning the JVM Version 
     return System.getProperty("sun.arch.data.model"); 
    } 

    public static void main(String[] args) throws InterruptedException { 
     String jacobDllVersionToUse; 
     System.out.println("JVM version is: " +jvmBitVersion()); 

     if (jvmBitVersion().contains("32")) { //Checking for the JVM Version 
      jacobDllVersionToUse = "jacob-1.18-M2-x86.dll"; // If the version is 32- bit use this. 
     } 
     else { // enter code here 
      jacobDllVersionToUse = "jacob-1.18-M2-x64.dll"; // if the version is 64-bit go for this 
     } 

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

     AutoItX x = new AutoItX(); 
     x.run("notepad.exe"); // trying to open the notepad 

     x.winActivate("Untitled - Notepad"); // waiting for the notepad to open 
     x.winWaitActive("Untitled - Notepad"); 

     x.send("This is some text"); // Once the notepad is open write into it. 

    } 
} 

如果代替notepad.exe我給calc.exe它工作正常。如果我在運行這個代碼後手動打開記事本,它會寫入記事本。

回答

2

立即開始記事本shown將解決此問題。

記事本

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"); 

Notepad++

AutoItX x = new AutoItX(); 
x.run("C:\\Program Files (x86)\\Notepad++\\notepad++.exe"); 
x.winActivate("[CLASS:Notepad++]"); 
x.winWaitActive("[CLASS:Notepad++]"); 
x.send("This is some text"); 
+0

謝謝。這對我有用。 –

+1

@AnubhavMishra我用Notepad ++的例子更新了我的答案。 – SubOptimal

1

嘗試的完整路徑:

x.run("C:\Windows\System32\notepad.exe") 

而且我會用不同的順序爲您等待命令,因爲您不能激活一個窗口,「不」的存在,這將是更明智的激活在使用send()命令之前的一個窗口。正確的順序應該是:

x.winWaitActive("Untitled - Notepad"); 

x.winActivate("Untitled - Notepad"); 
x.send("This is some text"); 

你也可以只使用WinWait()

1

此代碼的工作對我來說:

AutoItX x = new AutoItX(); 
x.run("notepad.exe", "C:/Windows/System32/", AutoItX.SW_SHOW); // trying to open the notepad 

x.winActivate("Sin título: Bloc de notas"); // waiting for the notepad to open 
x.winWaitActive("Sin título: Bloc de notas"); 
x.send("This is some text"); // Once the notepad is open write into it. 

注意,窗口標題必須與Windows應用程序相匹配。就我而言,Windows是西班牙文,標題是「Sintítulo:Bloc de notas」。