Sikuli Script in Java Part 1: Configuration and Hello World!

Part 1 of this series is dedicated to those of you looking to migrate away from the Sikuli IDE and are looking for a friendly guide to get you started.  For those of you already familiar with the basics of Sikuli Script in Java, much of this article will rehash what you already know, but go ahead and give it a read and feel free to provide me any useful feedback you might have!

In this post, I will cover:

  • Configuring your development environment
  • Creating your first Eclipse project
  • Your first Java Sikuli Script program

Configuring your development environment:

Before I go any further, I feel obligated to tell you that all of my experience is and will continue to be in Windows 7 x64, using the Eclipse Java IDE, so don’t expect to see any references to Macs or NetBeans in my posts!

If you’ve already successfully installed and played around in the Sikuli IDE, then you are just about where you need to be.  For those of you who are starting from scratch, then you will need the following:

  1. 32 bit Java JDK: Install the most recent version (currently 7) of the x586 Java JDK from Oracle’s website.  Once again, do *NOT* install the x64 JDK, as Sikuli looks for the 32-bit version.  Try the following link: http://www.oracle.com/technetwork/java/index.html
  2. Sikuli IDE: The Sikuli IDE includes the JAR file required to use Sikuli Script within your Java program.  Try the following link: http://sikuli.org/
  3. Java IDE: As I mentioned earlier, I recommend Eclipse.  Try the following link: http://www.eclipse.org/

Creating your first Eclipse project:

Now that the prereqs are out of the way, its time to fire up Eclipse and create your first Java project!

  1. The first thing you will need to do is start Eclipse and configure default Workspace.  If you are new to Eclipse but have some Visual Studio experience, think of the Workspace as your Solution.  For now, lets stick with the default presented with you.  At this point, you should be taken to your empty Workspace that is begging for a new project!SNAGHTML84de4f
  2. From here, you’ll want to create a new Java Project by navigating to “File > New > Java Project”. This will bring up the new Java Project wizard. In the dialog, type “HelloWorld” in the Project Name field name and click “Next” to continue. This will then take you to the Java Settings dialog.  From here, click the Libraries tab and click the “Add External JARs…” button.  Browse to the Sikuli install folder (example: C:\Program Files (x86)\Sikuli X) and select “sikuli-script.jar”.  You should now see “sikuli-script.jar” in your dialog as seen in the image below.  Click “Finish” and you are now ready to use Sikuli Script within your Java project!SNAGHTML3b337f
  3. If you want to follow along with my example below, you will need a screenshot of your Windows Start button and trim off any excess pixels as seen in my example below.image
  4. Within Package Explorer, right-click on the “HelloWorld” project folder and click “New > Folder” and name it “img”.  Now browse to this folder on your file system and save your Windows 7 Start button image as “Win7StartButton.jpg”.  Now refresh your Package Explorer and you will see the image appear as seen below.image
  5. It’s time to create your Java class where your code will reside.  Right click on “HelloWorld” and select “New > Class” and set the name to “HelloWorld” and check “public static void main..” and click “Finish”.  Now it’s finally time to do some coding!

SNAGHTML58709e

Your first Java Sikuli Script program

The following HelloWorld program demonstrates what are in my opinion, they most important classes within the Sikuli library; the Screen class and the Pattern class.  Take a look at the code below and try to give it a shot!  I hope my comments are sufficient and if you have any feedback, please send it to me so that I can update my code examples.

image

Bonus Round!

Now that you’ve completed the “HelloWorld” tutorial above, you might be wondering where to go next.  The following Java class is an overly simplified example of how my automation framework implements button clicking functionality.  The first image is the class itself and the second is a usage example.  Once again, these are purely academic in nature and are here to inspire you!  In closing, I hope you have enjoyed the first of the Sikuli Script in Java posts and once again, *please* send me any feedback so that I can improve these examples! – Paul Frymire

Class:image

Usage:image

10 thoughts on “Sikuli Script in Java Part 1: Configuration and Hello World!

  1. Pingback: Paul Frymire » Sikuli Script in Java Part 3: Basic Mouse Controls

  2. I have created a project following the steps provided by you.. but when i run this program i am getting the following error:

    Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\Users\vidisha.aggarwal\AppData\Local\Temp\tmplib\VisionProxy.dll: Can’t find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.load0(Unknown Source)
    at java.lang.System.load(Unknown Source)
    at com.wapmx.nativeutils.jniloader.NativeLoader.loadLibrary(NativeLoader.java:44)
    at org.sikuli.script.Finder.(Finder.java:33)
    at org.sikuli.script.Region.doFind(Region.java:1029)
    at org.sikuli.script.Region$RepeatableFind.run(Region.java:1232)
    at org.sikuli.script.Region$Repeatable.repeat(Region.java:1203)
    at org.sikuli.script.Region.exists(Region.java:538)
    at Helloworld.main(Helloworld.java:11)
    Please help me out what to do?

  3. Thank you for this post, it will be really very useful for such noobs as me)
    Waiting for continue!

  4. HI I’m working on a similar project and I tried your post, and I’m ending up with following error,
    Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\Program Files (x86)\Sikuli X\libs\Win32Util.dll: Can’t load IA 32-bit .dll on a AMD 64-bit platform
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(Unknown Source)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.load0(Unknown Source)
    at java.lang.System.load(Unknown Source)
    at com.wapmx.nativeutils.jniloader.NativeLoader.loadLibrary(NativeLoader.java:44)
    at org.sikuli.script.Win32Util.(Win32Util.java:14)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.sikuli.script.Env.getOSUtil(Env.java:84)
    at org.sikuli.script.ScreenHighlighter.init(ScreenHighlighter.java:175)
    at org.sikuli.script.ScreenHighlighter.(ScreenHighlighter.java:288)
    at org.sikuli.script.Screen.initBounds(Screen.java:96)
    at org.sikuli.script.Screen.(Screen.java:108)
    at HelloWorld.main(HelloWorld.java:19)

    I don’t understand this error. I have Installed 64 bit sikuli and I’m having a Intel processor, Dont know how it is showing AMD. Please help

  5. Sikuli has issues working with the 64-bit version of Java. If you have not already done so, install the 32-bit version of Java and give it another shot.

  6. hello Paul
    May i have a question, after step by step and run eclipse
    i didn’t see java click on start(i use win xp), i see out but console eclipse
    as 

    what happen for me please help

    import org.sikuli.script.*;

    public class hightlevel {

    public static void main(String[] args) {
    float similarity =(float) 0.8;

    int timeout =5;
    Pattern winxpStartButton =new Pattern(“img/winxpStartButton.png”);
    Screen screen =new Screen();

    if (screen.exists(winxpStartButton.similar(similarity),
    timeout)!=null )
    {
    try
    {
    screen.click(winxpStartButton, 0);
    }
    catch(FindFailed e)
    {
    e.printStackTrace();
    }
    }

    }
    }

Leave a Reply