user2957378
user2957378

Reputation: 637

Gradle: Fail on adding systemProperty

I'm trying to add a .dll file to the "java.library.path" system property via gradle on my Spring Boot project. I'm using Gradle 2.1 on STS. This is the small piece of groove code within my build.gradle:

tasks.withType(JavaCompile) {
    systemProperty "java.library.path", file("./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64")
}

And I'm getting the following error:

Could not find method systemProperty() for arguments [java.library.path, D:\GitHub\TFG_1\GuiaTV\src\main\resources\META-INF\opencv-2.4.9\windows_bin\x64] on root project 'GuiaTV'

That path does exists, so I don't know where the problem is.

Any help? Thank you!

UPDATE 1:

@Amnon Shochot What I try to do is to add a native library (.dll) to the project. I took the idea from some sites (for example, http://zouxifeng.github.io/2014/07/17/add-system-property-to-spring-boot.html, https://github.com/cjstehno/coffeaelectronica/wiki/Going-Native-with-Gradle).

The first one is using what you suggested:

tasks.withType(JavaExec) {
    systemProperty "java.library.path", file("./libs")
}

The second one is using:

run {
    systemProperty 'java.library.path', file( 'build/natives/windows' )
}

None of them are working for me. The first one (with JavaExec) is failing gradle test throwing:

java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)

If you follow the trace, it's crashing at runtime in sentence: System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

And the second one is failing on gradle build with the following message:

Could not find method run() for arguments [build_24sfpo0st6dokeq7fn3ad7r34$_run_closure7@2652c3da] on root project 'GuiaTV'.

Luckily you know exactly what I try to achieve and you can solve my problem.

Thank you for your interest!

UPDATE 2:

Finally, I ended up adding these lines to my build.gradle script:

// The following makes "gradle build", "gradle test" work
test {
    jvmArgs = ['-Djava.library.path=./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64']
}

// Thw following makes "gradle run" work
run {
    jvmArgs = ['-Djava.library.path=./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64']
}

By the way, I'm also using "spring-boot" gradle plugin. That's where the run task comes from. So, I can execute "gradle build", "gradle test" and "gradle run" sucessfully. This is, that native library is correctly added. However, since I'm also using "eclipse" gradle plugin, I would like to add the native library simply by executing "gradle eclipse". Instead, I must create the library on Eclipse manually, and add it to my project.

Thank you @Amnon for your collaboration. I'll be posting a new solution in the case I found it.

Upvotes: 1

Views: 3226

Answers (1)

Amnon Shochot
Amnon Shochot

Reputation: 9386

The problem is that you do not set the context for the systemProperty method thus Gradle tries to locate it in the project object where it does not exist which is the reason for the error you got.

If you wanted to apply this configuration for all tasks of type JavaCompile your code should have been looked like:

tasks.withType(JavaCompile) { JavaCompile t ->
    t.systemProperty "java.library.path", file("./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64")
}

However, the JavaCompile task type also does not contain a systemProperty so this code wouldn't work either.

You can define CompileOptions for a JavaCompile task using its options property, i.e.:

tasks.withType(JavaCompile) { JavaCompile t ->
    t.options "java.library.path", file("./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64")
}

However, I'm not sure whether you can define this specific system property.

One last note - the systemProperty method does exist for tasks of type JavaExec in case that this is what you intended to do.

Upvotes: 2

Related Questions