Search code examples
javagradletools.jar

Trying to use tools.jar - gradle


I am using gradle to package some java code into a jar. I am using some classes from tools.jar. I have had success in gradle building it and making a jar, but when I run that jar using java -jar <package>.jar I get the folowing java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine.

Since tools.jar is something you get with a jdk, not a jre. Is there a way I can bundle tools.jar with my package.jar and have my jar work anywhere?

Here is my build.gradle so far.

buildscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }    
}

description = "A java program"

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    flatDir {
        dirs System.properties['java.home'] + '/../lib'
    }
}


jar {
    archiveName = "jProg.jar"
    manifest {
        attributes(
                'Dependencies': 'com.sun.tools'
        )
    }
}


dependencies {
    compile group: 'com.sun', name: 'tools'

}

Solution

  • Probably what you need is called 'fat jar' (Gradle packs all dependencies to single jar)