HowTo Java: evaluate Sikuli ImagePath inside Eclipse and outside running from built jar

Asked by jeff radom

I've seen many questions like that but never understood how to fix it it. So, I'm able to run tests using Eclipse with Java but have a problem running the same tests using ant. I really need it that way because planning to run tests using Jenkins.

I saw the below suggestion
Sikuli cannot find the image files in the standard places it is looking for (so it tries to use the image filenames as text to search on the screen - I personally do not like this standard behavior ;-)

You have to use one of the available means to set the imagepath (e.g. addImagePath() or -DIMAGE_PATH=... on the java commandline).

But I don't understand what syntax or how can call addImagePath() method from java code. It looks like none of classes from Sikuli jar I use in my java code have the above method. I'll greatly appreciate any suggestions how to solve my problem

Thanks

Jeff

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

The respective class is
org.sikuli.basics.ImageLocator

Eclipse will show you the signature.

Revision history for this message
jeff radom (jradom) said :
#2

Hi RaiMan,

I did that but after I instantiated ImageLocator or tried to use addImagePath method eclipse showed it as unavailable. It'll be great if you can send me a code snippet how I can add a code which will allow to find images when running from ant. Also, it's not clear to me in what folder I can put images in order to run java scripts from ant even without pointing to a specific folder. I've tried many different ways without any success. I even add .sikuli folder and put all images in it and still no luck. I use Mac and eclipse with java. BTW, I can run my tests from eclipse with no problems

Thanks

Jeff

Sent from my iPad and will be short

> On Mar 3, 2014, at 2:01 AM, RaiMan <email address hidden> wrote:
>
> Your question #244845 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/244845
>
> Status: Open => Answered
>
> RaiMan proposed the following answer:
> The respective class is
> org.sikuli.basics.ImageLocator
>
> Eclipse will show you the signature.
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/sikuli/+question/244845/+confirm?answer_id=0
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/sikuli/+question/244845
>
> You received this question notification because you asked the question.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

this is a solution, that works inside Eclipse (running from the compiles classes) and outside running from the jar:

import java.io.File;
import java.io.IOException;
import java.security.CodeSource;
import org.sikuli.basics.ImageLocator;

public class Test {

  public static void main(String[] args) throws IOException {
    CodeSource src = Test.class.getProtectionDomain().getCodeSource();
    String jarpath = "---NOT_KNOWN---";
    if (src.getLocation() != null) {
      File jar = new File(src.getLocation().getPath());
      if (jar.getName().endsWith(".jar")) {
        jarpath = jar.getParent();
      } else {
        jarpath = jar.getParentFile().getParentFile().getAbsolutePath();
      }
    }
    String imgpath = new File(jarpath, "images").getAbsolutePath();
    System.out.println("images: " + imgpath);
    String workpath = System.getProperty("user.dir");
    System.out.println("work: " + workpath);
    ImageLocator.addImagePath(imgpath);
    System.out.println("test.png? " + ImageLocator.locate("test.png"));
  }
}

I tested this in Netbeans, where I have this project structure:
--- test project
    --- build/classes
    --- dist
    --- src
    --- images

when running in eclipse, the images folder is set to the one in the project (test/images)
when running from the jar, it is expected, that there exists a folder images in the folder containing the jar.

the rest is ant scripting,

Revision history for this message
jeff radom (jradom) said :
#4

Hi Raiman,

Unfortunately your suggestion didn't help.
This line never worked for me ImageLocator.addImagePath(
imgpath);
Adding of org.sikuli.basics.ImageLocator to my Eclipse project. Any method
in a ImageLocator class not available for me. It might work for a jar
file you use but with a one I got. So, can you please just email me your
Sikuli jar file. I've got mine by downloading from a sikuli.org site ante
installing. Or, if you suggest what particular subdirectory I can put my
images. I've tried many options including .sekuli but nothing worked. can
you suggest if istead of using ant we'll use maven for example can that fix
my issue or any other suggestion how to run using java, Mac OS and from a
server. I feel very bad about not having an easy way to run Sikuli from a
sever using Jenkins because I've suggested it to the team after
evaluation. No, I'm thinkg t o use something else or go back Selenium
which we used before. Please, help

Thanks

Jeff

On Mon, Mar 3, 2014 at 8:01 AM, RaiMan <<email address hidden>
> wrote:

> Your question #244845 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/244845
>
> Status: Open => Answered
>
> RaiMan proposed the following answer:
> this is a solution, that works inside Eclipse (running from the compiles
> classes) and outside running from the jar:
>
> import java.io.File;
> import java.io.IOException;
> import java.security.CodeSource;
> import org.sikuli.basics.ImageLocator;
>
> public class Test {
>
> public static void main(String[] args) throws IOException {
> CodeSource src = Test.class.getProtectionDomain().getCodeSource();
> String jarpath = "---NOT_KNOWN---";
> if (src.getLocation() != null) {
> File jar = new File(src.getLocation().getPath());
> if (jar.getName().endsWith(".jar")) {
> jarpath = jar.getParent();
> } else {
> jarpath = jar.getParentFile().getParentFile().getAbsolutePath();
> }
> }
> String imgpath = new File(jarpath, "images").getAbsolutePath();
> System.out.println("images: " + imgpath);
> String workpath = System.getProperty("user.dir");
> System.out.println("work: " + workpath);
> ImageLocator.addImagePath(imgpath);
> System.out.println("test.png? " + ImageLocator.locate("test.png"));
> }
> }
>
> I tested this in Netbeans, where I have this project structure:
> --- test project
> --- build/classes
> --- dist
> --- src
> --- images
>
> when running in eclipse, the images folder is set to the one in the
> project (test/images)
> when running from the jar, it is expected, that there exists a folder
> images in the folder containing the jar.
>
> the rest is ant scripting,
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/sikuli/+question/244845/+confirm?answer_id=2
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/sikuli/+question/244845
>
> You received this question notification because you asked the question.
>

Revision history for this message
RaiMan (raimund-hocke) said :
#5

I supposed you are using SikuliX version 1.0.1.
Having run setup and having got a sikuli-java.jar.

Is that true?

Be sure, this will be solved. Many other people are using Sikuli successful with Eclipse and Java.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

I do not have Eclipse on my Macs (I prefer Netbeans), but on my Windows 8 machine I have Eclipse Kepler.

I tested and had no problems (with version 1.0.1).

the zipped project folder you can download here:
https://dl.dropboxusercontent.com/u/42895525/TestEclipse.zip

the contained images folder has a test_output.png, that shows the Eclipse run output.

Revision history for this message
jeff radom (jradom) said :
#7

Yes i used sikuli-java.jar and also sekuli-script.jar. What is actually
the difference because manual is not clear

On Mon, Mar 3, 2014 at 11:41 PM, RaiMan <
<email address hidden>> wrote:

> Your question #244845 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/244845
>
> Status: Open => Needs information
>
> RaiMan requested more information:
> I supposed you are using SikuliX version 1.0.1.
> Having run setup and having got a sikuli-java.jar.
>
> Is that true?
>
> Be sure, this will be solved. Many other people are using Sikuli
> successful with Eclipse and Java.
>
> --
> To answer this request for more information, you can either reply to
> this email or enter your reply at the following page:
> https://answers.launchpad.net/sikuli/+question/244845
>
> You received this question notification because you asked the question.
>

Revision history for this message
RaiMan (raimund-hocke) said :
#8

sikuli-script.jar contains the Jython script running support and will no longer exist beginning with version 1.1.0

scripting (IDE + command line usages) will be supported by sikulix.jar and the usage of SikuliX with Java programming and Java aware programming and scripting languages is supported by sikulixapi.jar (which is the "same" as sikuli-java,jar now)
This was necessary, to finally get the sikuli stuff to Maven central later this year.

** about Maven/Ant: I completely switched to Maven now (knowing, that there are still many things, that are smoother with Ant). Until now I finally solved every challenge with Maven in my SikuliX project.

Revision history for this message
RaiMan (raimund-hocke) said :
#9

Have you run setup, to build a useable sikuli-java.jar (setup option 4)?

Every SikuliX stuff is only useable after having run setup.

Revision history for this message
jeff radom (jradom) said :
#10

Yes, I did run setup with option 1 which I think include all other options. So should use from now on sikuli-java.jar for my java code,correct? Also I wanted to ask how if can use OCR ant testng assertions. For example I want to assert if certain text appeared on a screen after a test finishes. Now I have to do it using image which is not very convenient.

Thanks

Jeff

Sent from my iPad and will be short

> On Mar 4, 2014, at 1:06 AM, RaiMan <email address hidden> wrote:
>
> Your question #244845 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/244845
>
> RaiMan posted a new comment:
> Have you run setup, to build a useable sikuli-java.jar (setup option 4)?
>
> Every SikuliX stuff is only useable after having run setup.
>
> --
> You received this question notification because you asked the question.

Revision history for this message
RaiMan (raimund-hocke) said :
#11

Pls. read carefully: you have to select option 4 (in words four), to get sikuli-java.jar.

I really recommend to repeat setup after purging the current setup folder (except sikuli-setup.jar).

... and pls. do not move around sikuli-java.jar. just leave it where it is and use references (e.g. in eclipse).

What about my Eclipse example?

Revision history for this message
jeff radom (jradom) said :
#12

It looks like adding PATH for images has finally worked for me in Eclipse
and ant thanks to your examples. Now, can I somehow use OCR with TestNG
asserts?

On Tue, Mar 4, 2014 at 9:06 AM, RaiMan <<email address hidden>
> wrote:

> Your question #244845 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/244845
>
> Status: Open => Needs information
>
> RaiMan requested more information:
> Pls. read carefully: you have to select option 4 (in words four), to get
> sikuli-java.jar.
>
> I really recommend to repeat setup after purging the current setup
> folder (except sikuli-setup.jar).
>
> ... and pls. do not move around sikuli-java.jar. just leave it where it
> is and use references (e.g. in eclipse).
>
> What about my Eclipse example?
>
> --
> To answer this request for more information, you can either reply to
> this email or enter your reply at the following page:
> https://answers.launchpad.net/sikuli/+question/244845
>
> You received this question notification because you asked the question.
>

Revision history for this message
RaiMan (raimund-hocke) said :
#13

To use the OCR feature
- you must have run setup with option 5 (additionally to option 4)
- if you have not done that before, you have to completely rerun setup from beginning
- at runtime in your Java app you have to say at the beginning:
org.sikuli.basics.Settings.OcrTextRead = true;

... and one time after setup you should delete the folder libs, so it gets recreated at runtime including the tessdata folder (a bug ;-)

Revision history for this message
jeff radom (jradom) said :
#14

I see, but how should i call OCR methods and use any sort of assertions
either TestNG or anything else?

On Tue, Mar 4, 2014 at 9:46 AM, RaiMan <<email address hidden>
> wrote:

> Your question #244845 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/244845
>
> Status: Open => Answered
>
> RaiMan proposed the following answer:
> To use the OCR feature
> - you must have run setup with option 5 (additionally to option 4)
> - if you have not done that before, you have to completely rerun setup
> from beginning
> - at runtime in your Java app you have to say at the beginning:
> org.sikuli.basics.Settings.OcrTextRead = true;
>
> ... and one time after setup you should delete the folder libs, so it
> gets recreated at runtime including the tessdata folder (a bug ;-)
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/sikuli/+question/244845/+confirm?answer_id=12
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/sikuli/+question/244845
>
> You received this question notification because you asked the question.
>

Revision history for this message
RaiMan (raimund-hocke) said :
#15

Sikuli's OCR feature is Region.text(), which returns a string with the text it could read.
So you have to somehow compare the returned text with your expectations and pack this into an assertion - but that is basic testing.

multiline text might make problems, as the complete OCR feature is still rather weak with some fonts.

Can you help with this problem?

Provide an answer of your own, or ask jeff radom for more information if necessary.

To post a message you must log in.