← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #295298]: How to add Image file in jar file

 

Question #295298 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/295298

    Status: Open => Answered

RaiMan proposed the following answer:
Ok, I never liked Eclipse and after having solved your case, I still do
not like it.

In the past I used NetBeans and now I use IntelliJ IDEA CE - both are
much more straight forward and especially IDEA has much more features
out of the box. Furthermore there is PyCharm, that especially with
SikuliX can be used, to develop and debug Jython scripts.

Nevertheless this is how it works in Eclipse:

Have a normal Java project and specify sikulixapi.jar in the project
properties -> Java Build Path -> libraries -> add external  jar

--- the main class file:
package testAPI;

import org.sikuli.basics.Debug;
import org.sikuli.script.ImagePath;
import org.sikuli.script.Match;
import org.sikuli.script.Screen;

public class Test {
	public static void main(String[] args) {
		Screen s = new Screen();
		Debug.info("Screen: %s", s);
		String clazz = "testAPI.Test";
		String imgFolder = "/imgs";
		String img = "test.png";
		String inJarFolder = clazz + imgFolder;
		if (ImagePath.add(inJarFolder)) {
			Debug.info("Image Folder in jar at: %s", inJarFolder);
		} else {
			Debug.error("Image Folder in jar not possible: %s", inJarFolder);
		}
		Match target = s.exists(img);
		if (null == target) {
			Debug.error("Not found: ", img);
		} else {
			Debug.info("Found: %s at %s", img, target);
			s.hover();
		}
		Debug.info("... leaving");
	}
}

--- the folders/files in the project:
-- src
   -- imgs
      test.png
   -- testApi
      test.java

--- running in project
using "run as Java application": works

--- making a jar and running that from commandline
there are 2 options that work:
-- option 1
let the final jar also contain sikulixapi.jar, so it is self contained
use the project -> export -> Java -> Runnable Jar file feature and select as "library handling" the first option (Extract required ...)
... you can run this from commandline using
java -jar <you-name-it>.jar

-- option 2
make a small jar with only your stuff and have sikulixapi.jar on classpath at runtime
There is no standard option to do just this, without touching the libraries (one of my points not loving Eclipse)
You have to select library handling option 3 (Copy ...). I strongly recommend, to directly delete the created lib folder again, to not have multiple copies of sikulixapi.jar on your system. 
... you can run this from commandline using
java -cp <path-to>\sikulixapi.jar;<you-name-it>.jar testAPI.Test

hope it helps

You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.