← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #244845]: Sikuli Cannot find images when running java, MAC OS, using ant

 

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,

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.