3

I am using phantomjs for headless browser testing. I call phantomjs from within my java file (MainTest.java). The code runs fine in my local machine. I bundle both the phantomjs.exe and the calling java file inside a jar file (tetsPhantom.jar).

  • testPhantom.jar
    • phantomjs.exe
    • MainTest.java

On the server, it is failing when it tries to find the phantomjs executable path:

String webDir = LiExportEmails.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm(); // Returns http://myservername.com:8080/testPhatom.jar
System.setProperty("phantomjs.binary.path", webDir);

I have tried setting it to http://myservername.com:8080/testPhatom.jar!phantomjs.exe as well, but it still fails.

What should be the correct path of phantomjs located inside the same jar file as the caller?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
contactabbas
  • 832
  • 2
  • 11
  • 18

2 Answers2

4

Solved it by using the approach from JNLP Webstart Launch Issue and Copying a binary file outside of a jar , with one change:

File f = new File ("./phantomjs.exe");
if (!f.exists()) {
  InputStream in = Myclass.class.getClassLoader().getResourceAsStream("phantomjs.exe");
  OutputStream out = new FileOutputStream("./phantomjs.exe");
  IOUtils.copy(in, out);
}
System.setProperty("phantomjs.binary.path","./phantomjs.exe");
Community
  • 1
  • 1
contactabbas
  • 832
  • 2
  • 11
  • 18
0

I really don't think it is possible to execute an .exe from within a jar file even if you could resolve the path somehow. You should extract the phantomjs.exe.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222