How to add Image file in jar file

Asked by Reshma Thakur

Hi,

I have added res folder and under that i have images folder and added all images inside that folder. so the structure is:

TPIM Project (main project)
   src
     TPIM (package)
       TPIM.java
   res (
     Images
       all images.png

src and res is in same level and both are resource folder.

here's the code:

  Pattern Username=new Pattern("res/Images/UserName.PNG");
  Pattern Password=new Pattern("res/Images/Password.PNG");

When i run it using eclipse it works fine, but when i make executable file/jar file, it doesn't recognize the username and password images. But i can see images folder in the jar file (when i open it using winrar).

Please suggest some solution. I am new with selenium, eclipse and sikuli. I have checked lots of website but could not find any ans. I have also check similar question in your website but that didn't solve my issue.

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1
Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#2

Hi RaiMan,

Thank you so much for the reply!!

I checked the link and tried adding below statement:

import org.sikuli.script.ImagePath; but this doesn't get recognized and gives error (underline with red line)

 Is my image folder structure correct?

Also, My eclipse version is Mars.2.Release (4.5.2) let me know if I am not using correct version. A

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#3

Any one ? Any suggestion will be appreciated.

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

I supposed you are using the latest SikuliX version:
http://sikulix.com

for versions before 1.1.0 it is not possible anyway

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#5
Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#6

Yes I am using sikulix 1.1.0.

My code is not able to recognize the source folder. When i debug it says source not fond( I have made res folder as source folder)

the folder structure is still same (mentioned in my question above):

Here's my code:

package TryImages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.script.*;
import org.sikuli.script.Screen;

import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class TryImages {

 static BufferedImage img;
 static BufferedImage img3;

 static String img1= "/imgs/Search.png";
 static String img2 ="/imgs/Enter.png";

 public static void main(String[] args) throws InterruptedException {

  Screen screen= new Screen();
  System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");

  WebDriver driver = new FirefoxDriver();

  driver.manage().window().maximize();

   System.out.println("checking file 1 ");
  driver.get("https://www.google.com/");

  try {
   System.out.println("inside try");
   screen.type(ImageIO.read(TryImages.class.getResource(img2)),"H");
   System.out.println("After Tyr");
  } catch (FindFailed e2) {
   System.out.println("find Fail");
   // TODO Auto-generated catch block
   e2.printStackTrace();
  } catch (IOException e2) {
   System.out.println("inside Fail");
   // TODO Auto-generated catch block
   e2.printStackTrace();
  }

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#7

https://wfi.egnyte.com/dl/gbJE65bFX1

I have uploaded my project in this link. If you get time please have a look and suggest what can i do to solve my issue.

Raiman, Appreciate you're help!

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

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

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#9

Thanks RaiMan, that solved my question.

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#10

Thank you very much Raiman!! this solved my problem. Really appreciated!!

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#11

Hi Raiman,

One question, so i added your code in my code and it ran till entering id, password and clicking submit.

Now, I have few pictures which need target offset property to specify where i want to click, so i did it like this:

   String Austinclk="Austin.PNG";
   String Scrollbar1="ScrollBar.PNG";

   Pattern Austin=new Pattern(Austinclk).targetOffset(-29, 0);

   Pattern Scrollbar=new Pattern(Scrollbar1);

when i run this code it gives below error message:

error] Image: could not be loaded: jar:file:/C:/Users/Reshma/Desktop/TPIM.jar!/imgs/Austin.PNG
[error] Image: Image not valid, but TextSearch is switched off!
[error] Image: could not be loaded: jar:file:/C:/Users/Reshma/Desktop/TPIM.jar!/imgs/ScrollBar.PNG
[error] Image: Image not valid, but TextSearch is switched off!
[error] Image: could not be loaded: jar:file:/C:/Users/Reshma/Desktop/TPIM.jar!/imgs/ScrollBar.PNG
[error] Image: Image not valid, but TextSearch is switched off!

Also, after clicking submit button, I need to click on Navigate button which comes after 10sec of clicking submit button, so when i do it directly from eclipse it works but using jar file it doesn't wait for a image to come in screen.

   String clazz = "testAPI.Test";
   String imgFolder = "/imgs";

   String inJarFolder = clazz + imgFolder;
   ImagePath.add(inJarFolder);
   String Navigate="NavigateBtn.PNG";

 s.click(s.wait(Navigate, 20));

it gives the below error message:

[error] Image: could not be loaded: jar:file:/C:/Users/Reshma/Desktop/TPIM.jar!/imgs/NavigateBtn.PNG
[error] Image: Image not valid, but TextSearch is switched off!
can't find navigate
[error] Image: could not be loaded: jar:file:/C:/Users/Reshma/Desktop/TPIM.jar!/imgs/NavigateBtn.PNG
[error] Image: Image not valid, but TextSearch is switched off!
Exception in thread "main" FindFailed: Region: doFind: Image not loadable: NavigateBtn.PNG
  Line 2535, in file Region.java

        at org.sikuli.script.Region.wait(Region.java:2535)
        at testAPI.Test.main(Test.java:163)

I am able to come so far because of your help, please suggest something to submit my project to client. Thanks a lot in Advance!

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

apparently SikuliX cannot find the imagefiles in the jar, so make sure, they are there and named as you did.

So check, wether the jar contains a folder imgs at rootlevel and that it contains imagefiles exactly named as you have it in your code.

Revision history for this message
Reshma Thakur (reshmasingh2089) said :
#13

Hi RaiMan,

Thanks for quick reply!

If i define pattern in the below format it works:

   String ReportLevel2="ReportLevel.png";

   Pattern ReportLevel=new Pattern(ReportLevel2).targetOffset(-149,183);

And I am able to load pages and my tool works fine. Thanks a lot for your help! Really Appreciated it.

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

user says it is solved