Skip to content
Advertisement

Exporting apache netbeans gradle project to jar file

I have created a Netbeans Gradle project, and I am trying to find the best way to export this to a .jar file so that it can be opened from outside the IDE. I am using dependencies within my project, and I was receiving a java.lang.NoClassDefFoundError.

To resolve this, I tried adding

jar {
manifest {
    attributes "Main-Class": "CollegeCounselor.Main"
}

from{

    configurations.compile.collect{ it.isDirectory() ? it : zipTree(it)}

    }
}

to my build.gradle file. However, this threw a java.lang.SecurityException: Invalid signature file digest for Manifest main attributes.

To fix this, I found a suggestion to add

{exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'}

to my file. This fixes the issue of running the file through the command line. Running it through java -jar works now. However, I still cannot run it through the finder on my mac. It gives me an error saying the java jar could not be launched and tells me to check the console for more details. I am unable to understand how to fix the issue, or why I can run it through the command line but not through the finder window. I am going to the build/libs/file.jar path and attempting to open this file.

Any assistance regarding the best way to export this project would be greatly appreciated.

Here is my full build.gradle file:

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


mainClassName = 'CollegeCounselor.Main'

repositories {
    jcenter()
}



dependencies {
    testCompile 'junit:junit:4.12'
    compile('com.microsoft.graph:microsoft-graph:1.5.+')
    compile group: 'com.microsoft.azure', name: 'msal4j', version: '1.4.0'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
}

jar {
    manifest {
        attributes "Main-Class": "CollegeCounselor.Main"
    }

    from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } } 
{
        exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
    }

}

Advertisement

Answer

This worked for me:

  • Create a simple Gradle application named gradleproject2 using the project wizard File > New Project… > Java with Gradle > Java Application.
  • Edit build.gradle based on this SO answer, to add the following entry:
jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'hello.HelloWorld'
    )
  }
}
  • Then replace hello.HelloWorld with your package and name of the class containing main(), which in my case was gradleproject2.Main, so my modified build.gradle looked like this:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'


mainClassName = 'gradleproject2.Main'

repositories {
    jcenter()
}

dependencies {
    testImplementation     'junit:junit:4.13'
}

jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'gradleproject2.Main'
    )
  }
}
  • Build your project which will put the jar file gradleproject2.jar in {project directory}buildlibs.

The effect of the change to build.gradle is to change the content of MANIFEST.MF within the jar file from:

Manifest-Version: 1.0

…to this:

Manifest-Version: 1.0
Class-Path:
Main-Class: gradleproject2.Main

Finally, run the jar file from the command line. In my case the command was simply java -jar gradleproject2.jar:

runJar

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement