Building executable jar with Gradle and NetBeans - java

I have searching to solve this problem for two days and I did not find solution. Firstly I wanted to build fat-jar, then I gave up on that it looked more complicated so I started building normal executable jar. I am building some program inside NetBeans 8.2 with Gradle plugin(http://plugins.netbeans.org/plugin/44510/gradle-support)
My project structure is like this:
Project Structure
I am relatively new to Gradle I am using it less then one month. My build.gradle looks like this:
repositories {
mavenCentral()
}
dependencies {
// TODO: Add dependencies here ..
// // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
// testCompile group: 'junit', name: 'junit', version: '4.10'
// https://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '3.16'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.16'
// https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.6'
}
apply plugin: 'java'
mainClassName = "paket.Glavna"
jar {
from {
(configurations.runtime).collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes("Main-Class": "Glavna" )
}
}
Now, I cant get it working to make executable jar. I think i have traced problem that there isnt path to main class in jar but I dont know why it isnt adding it. Error i get is:
Error: Could not find or load main class DnevniIzvestajG-1.0.jar
jar runs with (inside Glavna class is main):
java -cp projectname.jar paket.Glavna
Also I have tried running
manifest-addition
but taht also didnt add link to main class.

Try adding a package to the class with main() method.
jar {
archiveName = 'Glavna.jar'
manifest {attributes 'Main-Class': 'paket.Glavna'}
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {}
}
Building a fat jar like this is not preferable, try the Application plugin

I use below way to add all dependencies in .jar of gradle project.
build.gradle :-
jar {
manifest {
attributes(
'Main-Class': 'com.main.MainClass'
)
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

Related

Problems finding main class with gradle jar file

I've seen many people with "Could not find or load main class" errors when making jar files with Gradle. I have gone through many of the forums and have not found a solution that works for me yet. Following an online tutorial, I was able to set up a Gradle project in VSCode and add dependencies for a basic hello world program. Link to the tutorial I was following here: https://www.youtube.com/watch?v=BwdkyrnJQsg
This worked for me no problem but when I finished my much larger program, it doesn't find my Main.class file. Here's my build.gradle file:
plugins {
id 'java'
id 'java-library'
id 'application'
}
application
{
mainClass = 'Main'
}
java
{
sourceCompatibility = JavaVersion.VERSION_12
targetCompatibility = JavaVersion.VERSION_12
}
project.configurations.implementation.setCanBeResolved(true);
version = '1.0.0'
mainClassName = 'Main'
sourceSets
{
main
{
java
{
srcDirs 'src/main/java'
}
}
}
repositories
{
mavenCentral()
maven
{
url = 'https://licensespring-maven.s3.eu-central-1.amazonaws.com/'
}
}
dependencies
{
implementation 'com.licensespring:licensespring-license-client:2.1.0'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
// https://mvnrepository.com/artifact/net.harawata/appdirs
implementation group: 'net.harawata', name: 'appdirs', version: '1.2.0'
// https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform
implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '5.6.0'
// https://mvnrepository.com/artifact/net.java.dev.jna/jna
implementation group: 'net.java.dev.jna', name: 'jna', version: '5.6.0'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha1'
// https://mvnrepository.com/artifact/commons-io/commons-io
implementation group: 'commons-io', name: 'commons-io', version: '2.7'
}
jar
{
manifest
{
attributes(
"Main-Class": "$mainClassName",
"Class-Path": configurations.implementation.collect {it.name}.join(' ')
)
}
dependsOn('dependencies')
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
In src/main/java/ I have my Main.java file with the main method along with all my other .java files. When I run gradlew clean build, my .class files show up under build/classes/java/main.
Just to clarify, when I run java -jar './build/libs/myApplication.jar' I get the Could not find or load main class Main error. Let me know if you need any more specifics.
UPDATE: Switched over to Intellij again and I am getting errors involving the Main Manifest Attribute. I checked the outputted jar and the Manifest file is indeed packaged and it does have a Main Manifest Attribute. PLEASE HELP ME SOMEONE!
Can you try giving source set and full package name for main?
It should look something like this:
sourceSets {
main {
java {
srcDirs 'src/main/java'
}
}
}
project.configurations.implementation.setCanBeResolved(true)
jar{
manifest {
attributes(
"Main-Class": "com.xyz.Main",
"Class-Path": configurations.implementation.collect { it.name }.join(' ')
)
}
dependsOn ('dependencies')
}
I had similar issue to create runnable jar. I could solve it with following example
Solution 1 : Create a fat jar with all dependencies.
task fatJar(type: Jar) {
manifest {attributes 'Main-Class': 'com.example.gradle.App'}
from {
configurations.compile.collect
{ it.isDirectory() ? it : zipTree(it) }
}with jar
}
Solution 2 : Create a runnable jar with dependencies copied to a directory and setting the class path
def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
from configurations.compile
into "${dependsDir}"
}
task createJar(dependsOn: copyDependencies, type: Jar) {
manifest {
attributes(
'Main-Class': 'com.example.gradle.App',
'Class-Path': configurations.compile.collect
{ 'dependencies/' + it.getName() }.join(' ')
)}
with jar
}
Read more on this with an example here

Gradle jar will run inside of intellij but not outside as a standalone

After building my gradle jar within intellij, my project will run perfectly fine. If I try to then move that jar to my desktop and run it using the command "java -jar ProjectName.jar" it throws and error "Cannot find or load main class main"
I'm painfully aware that this question has been asked multiple times on this website as I've spent hours trying to fix it. Ever since I reinstalled Intellij I have been having this issue. I've tried making a fat jar and excluding META-INF, I tried creating a whole new project and creating a jar within that project. I also tried to use a recent iteration of this project which did run as a standalone and had no luck.
build.gradle
plugins {
id 'java'
}
group 'PantryDatabase'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile(
[group: 'com.google.cloud', name: 'google-cloud-bigquery', version: '1.74.0'],
[group: 'com.jfoenix', name: 'jfoenix', version: '9.0.8'],
[group: 'me.xdrop', name: 'fuzzywuzzy', version: '1.2.0']
)
}
jar {
manifest {
attributes 'Main-Class': "Main"
}
from configurations.runtime.collect { zipTree(it) }
}
task customFatJar(type: Jar) {
baseName = 'Pantry_Database'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
manifest {
attributes 'Main-Class': 'Main'
}
}
Class Files: https://imgur.com/gallery/HbGJPgr
Error Message: https://imgur.com/gallery/KkHNfas

Make jar with pom dependency in Gradle

I want to make an executable jar in Gradle:
task fatJar(type: Jar) {
manifest {
attributes (
'Main-Class': 'com.mycompany.json_validator.Main'
)
}
baseName 'json-validator'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
But i have an error from Gradle "Could not expand ZIP ..... archive is not a ZIP archive". I have this error on dependency, which is not .jar, it is pom.
compile group: 'org.glassfish.jersey', name: 'jersey-bom', version: '2.17', ext: 'pom'
How can i make an executable jar with it?
This appears to be the same issue described here: Gradle trying to unzip a pom typed dependency
The answer there describes what is happening and a possible remedy.
Alternatively, it should execute if you remove , ext: 'pom' from your dependency though I don't know if this is what you are going for.

Removing Jar Signatures in Gradle Build

We are having an issue with a war built from gradle failing to load in tomcat because of a Security Exception specific to a signed Jar. The stack trace is not showing what jar is causing the problem and to get this thing running I'm wondering if I can exclude the signatures in the build when the war is getting built but don't know how to do that with Gradle. In maven I believe it would be a <filter><exclude> tag but not sure if this type of thing is available in Gradle. Any input would be appreciated, the Exception being thrown is below.
Caused by: java.lang.SecurityException: Invalid signature file digest for
To find out if a jar file is signed, you can unzip the jar file using any zip utility tool. If the jar is signed it will contain files like *.RSA, *.SF or *.DSA under META-INF folder.
To exclude these signature files in gradle build , I did the following in my build.gradle. If you are using any other plugin to create the jar than you should check that plugins documentation for more details.
jar {
from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } } {
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
manifest {
attributes("Main-Class": "com.nk.social.shareit.streams.AppMain")
}}
My entire build.gradle file is as listed below:-
apply plugin: 'scala'
dependencies {
compile group: 'org.apache.kafka', name: 'kafka-streams', version: '0.11.0.1'
compile 'org.scala-lang:scala-library:2.12.2'
compile 'com.sksamuel.elastic4s:elastic4s-core_2.12:5.4.2'
compile 'com.sksamuel.elastic4s:elastic4s-http_2.12:5.4.2'
compile 'org.apache.lucene:lucene-core:6.5.1'
compile 'joda-time:joda-time:2.9.9'
testCompile group: 'org.scalatest', name: 'scalatest_2.12', version: '3.0.4'
}
jar {
from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } } {
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
manifest {
attributes("Main-Class": "com.nk.social.shareit.streams.AppMain")
}
}
Hope this helps.
With the jar, for example given in this thread I have a problem building a fat jar.
The main motivation is that the build process will skip also some dependecens.
A solution found is the following one, it contains only a small change
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'io.vincenzopalazzo.lightning.App'
)
}
from(configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
}

How do you build a .jar that contains external .jar dependencies?

I'm using the Microsoft JDBC Driver 4.1 to connect to SQL Server. In Eclipse I have added the .jar file to the class path of my project ( C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc41.jar).
I've added this .jar to a folder called lib in my project and am trying to have this .jar added as part of my .jar:
repositories {
mavenCentral()
}
dependencies {
compile files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
However, sqljdbc41.jar is not included in my .jar. Am I missing something?
You have to create a (application) distribution to package your libraries and application jar in a folder: http://gradle.org/docs/current/userguide/application_plugin.html
Alternatively you can use a plugin like https://github.com/johnrengelman/shadow to create a self-contained uberjar.
Thanks chromanoid. I was able to solve the problem two ways. First I placed the sqljdbc41.jar in a new folder called lib in my project. Solution 1 was to create an application distribution:
apply plugin:'application'
apply plugin: 'java'
mainClassName = "main.java.application.Main"
repositories {
mavenCentral()
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
You can then create a distribution using the distZip task. Solution 2 was to create a fat jar.
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
provided
compile.extendsFrom provided
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
dependsOn configurations.runtime
from {
(configurations.runtime - configurations.provided).collect {
it.isDirectory() ? it : zipTree(it)
}
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
and use the build task.

Categories