Skip to content

Commit 9e7511c

Browse files
committed
Handle the case when the runtime is in target/classes
Useful when testing com4j itself from IDE.
1 parent 13b3f81 commit 9e7511c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

runtime/src/main/java/com4j/COM4J.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ private static void loadNativeLibrary() {
542542
cause = t;
543543
}
544544

545+
final String fileName = "com4j-" + System.getProperty("os.arch") + ".dll";
546+
545547
// try loading com4j.dll in the same directory as com4j.jar
546548
URL res = COM4J.class.getClassLoader().getResource("com4j/COM4J.class");
547549
String url = res.toExternalForm();
@@ -567,7 +569,6 @@ private static void loadNativeLibrary() {
567569
e.printStackTrace();
568570
}
569571
File jarFile = new File(filePortion);
570-
String fileName = "com4j-" + System.getProperty("os.arch") + ".dll";
571572
File dllFile = new File(jarFile.getParentFile(), fileName);
572573
if(!dllFile.exists()) {
573574
// try to extract from within the jar
@@ -582,8 +583,15 @@ private static void loadNativeLibrary() {
582583
System.load(dllFile.getPath());
583584
return;
584585
}
586+
} else
587+
if(url.startsWith("file:")) {
588+
File classFile = new File(url.substring(5));
589+
File dllFile = new File(classFile.getParentFile(),fileName);
590+
System.load(dllFile.getPath());
591+
return;
585592
}
586593

594+
587595
UnsatisfiedLinkError error = new UnsatisfiedLinkError("Unable to load com4j.dll");
588596
error.initCause(cause);
589597
throw error;

0 commit comments

Comments
 (0)