Java Question Create a library: Need help to port AutoItX to B4J

jmon

Well-Known Member
Licensed User
Longtime User
Hello,

So I am trying to port the AutoItX4Java to B4J, because there are a few functions I need, and also because I think that it would be very useful for the community. I have never written code in Java, so I am bumping into problems.

This is my code (I am just testing with the CDTray function):
B4X:
package anywheresoftware.autoitx4b4j;

import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

import java.io.File;
import java.lang.System;
import com.jacob.com.LibraryLoader;
import autoitx4java.AutoItX;


/**
    *info here
*/
@Version(1.0f)
@ShortName("AutoItX")
public class AutoItX {

    // 1.0    initial release
    private static final double version = 1.0;

    /**
    *Returns the version number of the library.
    */
    public double getVersion() {
        return version;
    }

    AutoItX x;

    /**
    *Initialise an instance of AutoitX.
    */
    public void Initialize() {
        File file = new File("libs", "jacob-1.17-x64.dll"); //path to the jacob dll
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
        LibraryLoader.loadJacobLibrary();
        x = new AutoItX();
    }

    /**
    * Opens or closes the CD tray.
    *Parameters:
    *drive: The drive letter of the CD tray to control, in the format D:, E:, etc.
    *status: Specifies if you want the CD tray to be open or closed: "open" or "closed"
    */
    public void CDTray(String drive, String status) {
        x.run(Command); //note: this line is modified in post#3 and should read x.CDTray(drive, status);
    }
}

I built this code from the code provided by Agraham in the jDotNetBridge thread.

I get an error when I compile to a library (using the Simple library compiler):
B4X:
Starting step: Compiling Java code.
javac 1.7.0_45
C:\Program Files (x86)\Anywhere Software\Basic4android\SimpleLibraryCompiler\AutoItX\src\anywheresoftware\autoitx4b4j\AutoItX.java:15: error: anywheresoftware.autoitx4b4j.AutoItX is already defined in this compilation unit
import autoitx4java.AutoItX;
^
1 error


Error.

I can understand that I am trying to merge a library already merged, but I don't know where it has been merged before.

I also based myself on this code:
https://code.google.com/p/autoitx4java/
And this one:
https://stackoverflow.com/questions/11257784/java-with-autoit-and-jacob-library
and this:
http://letusautomatetest.blogspot.com/2012/08/ie-browser-pop-up-handling-to-support.html

If anyone could help, that would be very nice! I actually just need the WinGetState function from autoit, but I am trying to port the whole library for the community.

Thanks.

[edit] I uploaded the project to a zip file.
 

Attachments

  • AutoItX.zip
    144.4 KB · Views: 346
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Thank you. I managed to fix the problem. the libraries are loaded correctly I guess in eclipse. I just figured out that I just needed to comment this line and it works (Also there was a mistake in my CDTray function as it was executing the run function instead.

So I managed to compile the library, and now I have loaded the library in B4J, but I now run into other problems
B4X:
Program started.
main._appstart (java line: 53)
java.lang.NoClassDefFoundError: com/jacob/com/LibraryLoader
    at anywheresoftware.autoitx4b4j.AutoItX.Initialize(AutoItX.java:39)
    at b4j.example.main._appstart(main.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:33)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: com.jacob.com.LibraryLoader
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 20 more
I upload the latest file, with the changes I mentioned.
 

Attachments

  • AutoItX.zip
    145.7 KB · Views: 352

jmon

Well-Known Member
Licensed User
Longtime User
Well I am making some progress, I commented the line:

B4X:
LibraryLoader.loadJacobLibrary();

and it works better, I can initialize, but I get an error "java.lang.NullPointerException" when I try to click on the button:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim x As AutoItX
    Private btnOpen As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    x.Initialize
    Log("Version: " & x.Version)
End Sub

Sub btnOpen_Action
    x.CDTray("E:", "open")
End Sub

B4X:
Program started.
Version: 1
main._btnopen_action (java line: 62)
java.lang.NullPointerException
    at anywheresoftware.autoitx4b4j.AutoItX.CDTray(AutoItX.java:50)
    at anywheresoftware.autoitx4b4j.AutoItX.CDTray(AutoItX.java:50)
    at b4j.example.main._btnopen_action(main.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA$2.run(BA.java:162)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Yessss , I finally made it! I am so happy, I spent the whole day on it! My first java test ever!

This is my java code:
B4X:
package anywheresoftware.jmon;

import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

import java.io.File;
import java.lang.System;
import com.jacob.com.LibraryLoader;
import autoitx4java.AutoItX;


/**
    *info here
*/
@Version(1.0f)
@ShortName("jAutoItX")
@DependsOn(values={"AutoItX4Java","jacob"})
public class jAutoItX {

    // 1.0    initial release
    private static final double version = 1.0;

    /**
    *Returns the version number of the library.
    */
    public double getVersion() {
        return version;
    }

    autoitx4java.AutoItX aix;

    /**
    *Initialize an instance of AutoitX.
    */
    public void Initialize() {
        File file = new File("lib", "jacob-1.17-x86.dll"); //path to the jacob dll
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
        aix = new autoitx4java.AutoItX();

    }

    /**
    * Opens or closes the CD tray.
    *drive: The drive letter of the CD tray to control, in the format D:, E:, etc.
    *status: Specifies if you want the CD tray to be open or closed: "open" or "closed"
    */
    public boolean cdTray(String drive, String status) {
        return (aix.cdTray(drive, status));
    }

    public boolean mouseMove(int x, int y, int speed) {
        return (aix.mouseMove(x, y, speed));
    }

}

Compile the Java code with Simple Library Compiler, Then, in you B4J project, put the "jacob-1.17-x86.dll" in the folder ./Objects/lib (you need to create the folder 'lib'). I think I need to use the X86 version because my java version is x86, eventhough my OS is x64.

In B4J use this code:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim x As jAutoItX
    Private btnOpen As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    x.Initialize
    Log("Version: " & x.Version)
End Sub

Sub btnOpen_Action
    x.cdTray("E:", "open")
    x.mouseMove(10, 10, 1)
End Sub

It works. I will write the rest.
 

Theera

Expert
Licensed User
Longtime User
Hi jmon,or others
I've followed your steps,I've created the library from post#1 and post#5 ,I 've accomplished for library creating which attached. After then I use your B4J's sourcecode in post#5,but I've error. How to get rid of the problem.



P.S. I use Windows8Pro x86,JDK8
 

Attachments

  • jAutoItX.zip
    59.4 KB · Views: 325
  • TestjAutoItX.zip
    69.9 KB · Views: 324
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Sawasdee krap,
I have just released the jAutoItX Library here : http://www.b4x.com/android/forum/threads/jautoitx.37539/
Please follow the steps I provided there and tell me there if you have any more problems. Thanks.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…