3

So I have a project over here that has the following build.gradle:

// PLUGINS

plugins {
  id 'java' // or 'groovy' Must be explicitly applied
  id 'com.github.johnrengelman.shadow' version '1.2.2'
}

apply plugin: 'application'
apply plugin: 'java'

// REPOSITORIES & DEPENDENCIES

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'joda-time:joda-time:2.9'
    compile 'com.miglayout:miglayout-swing:5.0'
    compile 'com.dorkbox:SystemTray:1.9'
    compile 'com.dorkbox:SystemTray-Dorkbox-Util:1.9'
    compile 'net.java.dev.jna:jna:4.2.1'
    compile 'org.slf4j:slf4j-simple:1.7.5'
    compile 'commons-cli:commons-cli:1.3.1'
}

// SETTING THE MAIN CLASS

mainClassName = "com.github.tgharib.Program"

jar {
    manifest {
        attributes 'Main-Class': 'com.github.tgharib.Program'
    }
}

Currently, following these build instructions compiles the application just fine. However, for the com.dorkbox:SystemTray library, I have to include SystemTray's transitive dependencies: net.java.dev.jna:jna and org.slf4j:slf4j-simple. If I understand correctly, Gradle automatically includes transitive dependencies by default but if I remove the compile 'net.java.dev.jna:jna:4.2.1' line for example, my program still compiles but it fails to run (because JNA is a runtime dependency).

I've spent about 3 hours trying to fix it but I haven't been able to solve it. One SO user suggested to clear the cache as that fixed the issue for him but clearing the cache didn't fix the issue. I also learned about the gradle dependencies command. Here's the output:

:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

compile - Compile classpath for source set 'main'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

default - Configuration for default artifacts.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

runtime - Runtime classpath for source set 'main'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

shadow
No dependencies

testCompile - Compile classpath for source set 'test'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

testRuntime - Runtime classpath for source set 'test'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

Total time: 4.544 secs

So as you can see, SystemTray does indeed include JNA as a dependency but if I remove the JNA dependency in my main project, it fails to run.

Taha
  • 51
  • 1
  • 4
  • did you try -d (debug) option to see what's actually happening? also, what command do you use to build the jar? – AdamSkywalker Dec 11 '15 at 16:28
  • @AdamSkywalker To build the JAR, I used the command `gradle shadowJar`. However, the same issue occurs even with `grade run`. If I include JNA as a dependency, `gradle run` runs it just fine but if I don't, `gradle run` throws a runtime dependency. Here's the output of `gradle -d run` without the `compile 'net.java.dev.jna:jna:4.2.1'` line: http://pastebin.com/raw.php?i=c9VfL9BE – Taha Dec 11 '15 at 16:43
  • It appears these are the relevant lines: http://pastebin.com/raw.php?i=N5pE1M1C so it appears to be fetching JNA but the program fails to run, hmmm – Taha Dec 11 '15 at 16:44
  • try gradle build shadowJar and tell if it works – AdamSkywalker Dec 11 '15 at 16:57

1 Answers1

2

It ended up being an issue with the pom file upstream. They switched the dependency from compile-time dependency to run-time dependency and that fixed it.

enter image description here

https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
Taha
  • 51
  • 1
  • 4
  • 1
    I come into the same problem, can you give me more details? – jerry_sjtu Feb 27 '17 at 03:19
  • It looks like this change is made by replacing `implementation` with `api` in the helper library's transitive dependency. So if you have A -implementation-> B -implementation-> joda-time, replace the inner part with with B-api->joda-time. BUT, this still doesn't solve my issue :) – Sridhar Sarnobat Dec 06 '22 at 22:56