1

I have a gradle project which is using sqlite4java and I'm working in eclipse.

My problem is that when I get gradle to generate the eclipse project files, the project classpath contains sqlite4java's native libraries on the classpath instead of on the java.library.path which causes eclipse to fail to build my project because it complains that the native libraries are not well formed zip files. Also, when I manually remove the native libraries from the classpath, then when i run the tests or the application they error because they fail to load the sqlite4java native libraries.

How do I get gradle to set the java.library.path in eclipse for sqlite4java so that my code complies and runs from within eclipse?

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Alex Q
  • 3,080
  • 2
  • 27
  • 29

1 Answers1

1

Found the solution on this thread: https://discuss.gradle.org/t/is-it-possible-to-set-eclipses-java-library-path-from-build-gradle/6511/6

Adding the following code to my gradle build file and re-running gradle eclipse solved the issue:

build.gradle:

def getSqlLite4JavaNativeLibraryPath() {
    return configurations.runtime.resolve().findResult { entry ->
        String absolutPath = entry.getAbsolutePath();

        if(absolutPath.contains("sqlite4java-win32-x64")){
            // return the directory that contains the native library
            return entry.getParent()
        }
    }
} 

eclipse.classpath.file.whenMerged { classpath ->
    //remove the all native libraries as direct dependencies
    classpath.entries.removeAll { 
        entry -> entry.kind == 'lib' && (entry.path.endsWith('.dll') 
            || entry.path.endsWith('.so')
            || entry.path.endsWith('.dylib'))
    }
    //but add them as native libraries
    def sqlite4java = classpath.entries.findResult { entry ->
        if (entry.kind == 'lib' && entry.path.contains('sqlite4java')) {
            return entry
        }
    }
    sqlite4java.setNativeLibraryLocation(getSqlLite4JavaNativeLibraryPath())
}

The generated .classpath file now contains the attributes for the native library directory.

.classpath:

<classpathentry sourcepath="C:/Users/user1/.gradle/caches/modules-2/files-2.1/com.almworks.sqlite4java/sqlite4java/1.0.392/2efe18f7bea6fa9536802dd4ea54d948117216c6/sqlite4java-1.0.392-sources.jar" kind="lib" path="C:/Users/user1/.gradle/caches/modules-2/files-2.1/com.almworks.sqlite4java/sqlite4java/1.0.392/d6234e08ff4e1607ff5321da2579571f05ff778d/sqlite4java-1.0.392.jar" exported="true">
    <attributes>
        <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="C:\Users\user1\.gradle\caches\modules-2\files-2.1\com.almworks.sqlite4java\sqlite4java-win32-x64\1.0.392\d20dc00abecc7e0bde38c68eee68f2e70c26df95"/>
    </attributes>
</classpathentry>

I also ran into the problem that the gradle unit tests were failing to load the sqlite4java native libraries, so I also needed to add the following to my gradle test target so that the tests run from gradle would be able to load the sqlite4java native libraries.

test {
    systemProperty "sqlite4java.library.path", getSqlLite4JavaNativeLibraryPath()
}
Alex Q
  • 3,080
  • 2
  • 27
  • 29