[Java] Apache Ant: ExceptionInInitializerError using Sikuli --- global path settings not recognized

Asked by Chaminda Peiris

*** the solution ***
I use Apache Ant to build and run the tests. It turned out that even though the PATH is correctly set on the shell running the ant command, AN does not pass it correctly. Therefore I had to modify the ant target to manually set it.

<exec executable="java">
   <arg line="-cp ${sikuli.classpath} com.hmh.qa.test.TestCycleTest"/>
   <env key="Path" path="${env.Path}${path.separator}${jre.bin}${path.separator}${sikuli.dir}/lib"/>
</exec>
-------------------------------------------------------

I'm trying to run some simple Sikuli code through java, and I get an exception when Sikuli is being initialized. The following is logged.

Windows utilities loaded.
Sikuli vision engine loaded.
Text Recgonizer inited.

Then I get the stack trace below.

 N/A

java.lang.ExceptionInInitializerError
at org.sikuli.script.Finder.(Finder.java:22)
at org.sikuli.script.Region.findNow(Region.java:589)
at org.sikuli.script.Region.wait(Region.java:277)
at org.sikuli.script.Region.find(Region.java:235)
at org.sikuli.script.Region.getLocationFromPSRML(Region.java:652)
at org.sikuli.script.Region.click(Region.java:346)
at com.hmh.qa.test.TestCycleTest.takeAssessment(TestCycleTest.java:97)
at com.hmh.qa.test.TestCycleTest.testStudentTakesTest(TestCycleTest.java:85)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
at java.io.Reader.(Reader.java:61)
at java.io.InputStreamReader.(InputStreamReader.java:55)
at org.sikuli.script.ResourceExtractor.writeFileList(ResourceExtractor.java:23)
at org.sikuli.script.ResourceExtractor.extract(ResourceExtractor.java:17)
at org.sikuli.script.TextRecognizer.init(TextRecognizer.java:30)
at org.sikuli.script.TextRecognizer.(TextRecognizer.java:24)
at org.sikuli.script.TextRecognizer.getInstance(TextRecognizer.java:45)
at org.sikuli.script.TextRecognizer.(TextRecognizer.java:16)

The code triggering the exception is given below. Line 97 that generates the exception is the screen.click() statement

  Screen screen = new Screen();
  screen.click("etc/imgs/English.png", 0);

I have done all the pre-requisites as suggested.

1. sikuli-script.jar is in the classpath
2. C:\Program Files\Java\jre6\bin;C:\Program Files\Sikuli X\libs are in the path

I have a python script of the same code. It runs from the command line with the following command.

java -jar target\lib\sikuli-script.jar sikuli.sikuli

The environment details are given below
OS: Win XP (SP3)
Java: 6
Sikuli: Sikuli-X-1.0rc1-win32.exe

Please help me resolve the issue. I need the code to run from Java since it is within a test case already automated using Selenium RC.

Question information

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

sure, that the image can be found?

Try an absolute path to the image.
or use Settings.BundlePath to set it to the directory, where the images are
or use setImagePath()

Since I do not have the Java experiences, I dare to ask:
shouldn't it be backslashes in the image filename?

Revision history for this message
Chaminda Peiris (cpeiris) said :
#2

The image path is not the issue. I have tried with the following.

1. Providing the relative path with back slashes
2. Providing absolute path
3. Copying the images to the root folder and providing the image location as c:\imagename.png

All three results in the same error.

My guess is that the problem could be in the initialization. When I run the .py script in the command line I get the following lines logged to stdout before the test runs correctly.

Sikuli vision engine loaded.
Text Recgonizer inited.
Windows utilities loaded.
VDictProxy loaded.
EventManager loaded.

However when I run the Java Class, I only get the following logged to stdout.

Windows utilities loaded.
Sikuli vision engine loaded.
Text Recgonizer inited.

So the issue could be in initializing the VDictProxy or EventManager

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

That the .py code is running means, that Sikuli with all its features is running on your machine.

Have you already tried to get a simple java main with these two statements running from commandline, without your application specific Java environment?

Has this been running with Sikuli 10.2 before?

Revision history for this message
Chaminda Peiris (cpeiris) said :
#4

Did a lot of digging and finally got to the bottom of it....

as you suggested I got the two lines extracted the main() method of a class and ran it through the command line. It worked. So that means the problem was in my project settings. I use Apache Ant to build and run the tests. It turned out that even though the PATH is correctly set on the shell running the ant command, AN does not pass it correctly. Therefore I had to modify the ant target to manually set it.

<exec executable="java">
   <arg line="-cp ${sikuli.classpath} com.hmh.qa.test.TestCycleTest"/>
   <env key="Path" path="${env.Path}${path.separator}${jre.bin}${path.separator}${sikuli.dir}/lib"/>
</exec>

I also modified the code to set the image path.

ImageLocator imageLocator = new ImageLocator();
imageLocator.addImagePath("file:///c:/imgs");
screen.click("English.png", 0);

Everything works now.

To answer your final question, I have not used SiKuli before.

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

great research. many thanks. this should help others.

I will edit the question accordingly.

Have fun and success with Sikuli ;-)

Revision history for this message
Ben Sullivan (8-mailbox) said :
#6

Great post thanks guys!!