4

To use an external com object I have to include the Jacob jar and dll library into my Jenkins plugin. I found the jacob_excel_reader plugin which use Jacob, but the solution doesn't satisfy me. The developer added the jar dependency to the pom.xml file:

<dependency>
  <groupId>com.jacob</groupId>
  <artifactId>jacob</artifactId>
  <version>1.17-M2</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/jacob.jar</systemPath>
</dependency>

and integrates the dll file via the argument:

-Djava.library.path=${workspace_loc:jacob_excel_reader}\lib

Is this the only way to integrate a dll? I tried it another way but I got stuck halfway and I definitely need your help!

First I added the dependency for the jar and dll file to the pom.xml:

<dependency>
  <groupId>net.sf.jacob-project</groupId>
  <artifactId>jacob</artifactId>
  <version>1.14.3</version>
</dependency>
<dependency>
  <groupId>net.sf.jacob-project</groupId>
  <artifactId>jacob</artifactId>
  <version>1.14.3</version>
  <classifier>x86</classifier>
  <type>dll</type>
</dependency>

and configure that the dll file will be copied to target/APPNAME/WEB-INF/lib:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <excludeTransitive>true</excludeTransitive>
            <includeArtifactIds>jacob</includeArtifactIds>
            <includeTypes>dll</includeTypes>
            <failOnMissingClassifierArtifact>true</failOnMissingClassifierArtifact>
            <silent>false</silent>
            <outputDirectory>target/APPNAME/WEB-INF/lib</outputDirectory>
            <overWriteReleases>true</overWriteReleases>
            <overWriteSnapshots>true</overWriteSnapshots>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

When I run mvn package the default tests throw a warning:

SEVERE: Failed Inspecting plugin C:\...\AppData\Local\Temp\hudson7697228528744044800tmp\the.jpl
java.util.zip.ZipException: error in opening zip file

but it copies the dll and jar file to target/APPNAME/WEB-INF/lib. Why does it copy the jar file too and what causes the ZipException?

During the plugin execution the plugins throws:

FATAL: no jacob-1.14.3-x86 in java.library.path
java.lang.UnsatisfiedLinkError: no jacob-1.14.3-x86 in java.library.path

and cannot find the dll file, why? Can I add target/APPNAME/WEB-INF/lib to the java.library.path without using the argument above?

Sascha Vetter
  • 2,466
  • 1
  • 19
  • 36

1 Answers1

2

In general, what you need to do is get hold of the path the DLL resides in and then give Jacob the hint where to find the path.

Regarding the first step, this could work:

String relativeWebPath = "/WEB-INF/lib/";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);

Alternatively, if, e.g., your DLL file is packed in a JAR, you'd have to extract it first to a known location.

InputStream in = SomeClassInTheJAR.class.getResourceAsStream("/jacob-1.17-x86.dll");
File fileOut = new File(tempDir + "/jacob-1.17-x86.dll");
String absoluteDiskPath = tempDir;

OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();

Regarding, the second step, to make the DLL path known to Jacob, do this:

System.setProperty(com.jacob.com.LibraryLoader.JACOB_DLL_PATH, absoluteDiskPath);

However, it is not very much better than the command line flag. Just different ... It seems there is no "easy" (as in out-of-the-box) way to load the DLL from within a WebApp in general, or from within a Jenkins Plugin in particular.

Stefan Winkler
  • 3,871
  • 1
  • 18
  • 35
  • I used the code snippet (and changed `getServletContext()` to `Jenkins.getInstance().servletContext`) but it throws `FATAL: ...\target\work\webapp\WEB-INF\lib: Can't find dependent libraries java.lang.UnsatisfiedLinkError: ...\target\work\webapp\WEB-INF\lib: Can't find dependent libraries` – Sascha Vetter Oct 15 '13 at 15:23
  • Is there any solution ? – dan_l Mar 18 '14 at 23:20