gradle executable jar can't include local jar dependencies - java

SOLVED
WaitingDatabase.connect(WaitingDatabase.java:17)
// added this line
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(
DatabaseProperties.properties.getProperty("waiting_url_oracle"),
DatabaseProperties.properties.getProperty("waiting_user_oracle"),
DatabaseProperties.properties.getProperty("waiting_password_oracle")
);
(Sorry for bad english.)
I have to connect my oracle database, so I downloaded oracle jdbc driver and added in my dependencies. It connected well when I run at intellij, so I built executable jar file with gradle.
However, it couldn't connect to my oracle database. I think it was built without oracle jdbc driver file, because it is a local jar file.
How can I build with all of my dependencies?
If you need more information about my project to solve this problem, please let me know.
Thank you :D
build.gradle
jar {
manifest {
attributes 'Main-Class': 'App'
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
// https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.6.0'
// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
// oracle 11g jdbc driver
compile files('lib/ojdbc6.jar')
}
cmd
{directory}> java -jar ./{jarFileName}.jar
error message
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#{address}:{port}:{sid}
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at WaitingDatabase.connect(WaitingDatabase.java:17)
at App.connectDatabase(App.java:22)
at App.main(App.java:5)
WaitingDatabase.connect(WaitingDatabase.java:17)
connection = DriverManager.getConnection(
DatabaseProperties.properties.getProperty("waiting_url_oracle"),
DatabaseProperties.properties.getProperty("waiting_user_oracle"),
DatabaseProperties.properties.getProperty("waiting_password_oracle")
);
file tree
- {root directory}
- lib
- ojdbc6.jar
- src
- main
- java
- App.java
- WaitingDatabase.java
- build.gradle
ADDED
db.properties
sleep-millisecond=1000
waiting_db=oracle
waiting_url_oracle=jdbc:oracle:thin:#{address}:{port}:{sid}
waiting_user_oracle={username}
waiting_password_oracle={password}
waiting_url_mariadb=jdbc:mariadb://{address}:{port}
waiting_user_mariadb={username}
waiting_password_mariadb={password}

What you did is correct:
compile files('lib/ojdbc6.jar')
This will also work:
dependencies {
implementation fileTree(dir: 'lib', include: '*.jar')
The advantage of this is that it will include all the jars in the lib directory without you having to do it manually.
ojdbc6.jar is meant for JDK 6, as stated here.
If you are using > JDK 6, you might want to consider upgrading your ojdbc6.jar and most importantly you might want to check this.
Cheers

Related

Building a Jar using gradle - Jar hasn't sql dependency [duplicate]

This question already has answers here:
Using Gradle to build a JAR with dependencies
(19 answers)
Closed last year.
I am trying to build a very simple Program.
I only have one dependency, the msql-jdbc.
Every build succeds. But if i try to run the jar this exception ist thrown:
Error: Could not find or load main class metaDataDB-0.5-msSQL.jar
Caused by: java.lang.ClassNotFoundException: metaDataDB-0.5-msSQL.jar
If i look into the jar, for example 7zip, I see all my compiled Classes.
The content of MANIFEST.MF is as follows:
Manifest-Version: 1.0
Main-Class: metaDataDB.Main
Maybe this helps:
Project Structure in Intellij
Here is my full build:
plugins {
id 'java'
}
jar{
manifest {
attributes 'Main-Class': 'metaDataDB.Main'
}
}
repositories {
mavenCentral()
}
group 'com.dvb'
//version '1.0-SNAPSHOT'
version '0.5-msSQL'
sourceCompatibility = 17
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
// https://mvnrepository.com/artifact/org.postgresql/postgresql
//implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
// / https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
// https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
implementation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.5.0.jre17-preview'
}
test {
useJUnitPlatform()
}
Did I setup something wrong? Is this an Intellij error?
I am really confused. I think it should work...
EDIT:
I did the mistake that i did a wrong java command.
The way i did it before:
java metaDataDB-0.5-msSQL.jar -login:login.txt
The right way to do it:
java -jar metaDataDB-0.5-msSQL.jar -login:login.txt
But know I have another Problem. My jdbc driver isn't included in the Jarfile
the problem exists in msSQL.jar, there are some missing classes I guess, you can go to https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc and Implement the right jar for your JRE or experiment with different versions.
The first problem was,
that I ecexuted the Jar file in a wrong way.
I did
java metaDataDB-0.5-msSQL.jar -login:login.txt
The right way to do it:
java -jar metaDataDB-0.5-msSQL.jar -login:login.txt
After that, I realized that I didn't include the dependency needed into my Jar.
I've learned what a Fat Jar File is.
So I needed to add this to my gradle jar task:
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it: zipTree(it)}
}
For referencere my whole build.gradle file:
plugins {
id 'java'
}
jar{
manifest {
attributes ('Main-Class': 'metaDataDB.Main')
}
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it: zipTree(it)}
}
}
repositories {
mavenCentral()
}
group 'com.dvb'
//version '1.0-SNAPSHOT'
version '0.5-msSQL'
sourceCompatibility = 17
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
// https://mvnrepository.com/artifact/org.postgresql/postgresql
// implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
implementation 'com.microsoft.sqlserver:mssql-jdbc:10.1.0.jre17-preview'
// / https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
// https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
//implementation 'com.microsoft.sqlserver:mssql-jdbc:9.2.1.jre15'
}
test {
useJUnitPlatform()
}
That worked for the postgressql Driver.
For the Microsoft SQL Drivers a new Problem came up.
java -jar metaDataDB-0.5-msSQL.jar -login:ms.txt
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at java.base/sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:340)
at java.base/sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:282)
at java.base/java.util.jar.JarVerifier.processEntry(JarVerifier.java:277)
at java.base/java.util.jar.JarVerifier.update(JarVerifier.java:234)
at java.base/java.util.jar.JarFile.initializeVerifier(JarFile.java:762)
at java.base/java.util.jar.JarFile.ensureInitialization(JarFile.java:1033)
at java.base/java.util.jar.JavaUtilJarAccessImpl.ensureInitialization(JavaUtilJarAccessImpl.java:72)
at java.base/jdk.internal.loader.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:883)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:848)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:780)
at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:675)
here a link to the exception:
https://learn.microsoft.com/de-de/dotnet/api/system.security.securityexception?view=net-6.0
I tried it to run in adminstrator mode, but that failed too...
But I guess this is a new question, and hasn't to do anything with gradle.
I guess the answer to "Building a Jar using gradle - Jar hasn't sql dependency" is answered.
I will create a new Question for the microsoft sql driver and link it here.
(I hope I did everything right)

Gradle: publish existing jar/source/javadoc to maven

I have some proprietary libraries which are used in my java project. I use gradle as build manager. I use the eclipse-ide.
Since I dont want to have any binaries in my repo, I created a custom maven repository. Prior to Gradle 6.x Gradle just downloaded the .jar file with the correct folder structure in the repository (GROUPNAME/ARTIFACT/VERSION/... .jar)
After I updated to Gradle 6.5 Gradle wanted a .pom file to specify the jar. So I created a dummy project with this build file and used the command gradle install to get the .pom file.
plugins {
id 'java-library'
id 'maven'
}
repositories {
jcenter()
}
dependencies {
api group: 'commons-codec', name: 'commons-codec', version: '1.10'
api group: 'commons-logging', name: 'commons-logging', version: '1.2'
api group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
api group: 'log4j', name: 'log4j', version: '1.2.17'
}
install {
repositories.mavenInstaller {
pom.version = '...'
pom.groupId = '...'
pom.artifactId = '...'
}
}
With this method I had to rename the library jar accordingly. Now I have the .pom file but missing all the md5/sha/metafile/... files which are normaly generated with the maven-publish plugin.
I either need to know how to configure my dummy project to generate the md5/sha/metafile/... without having the source code - just have the .jar/sources.jar/javadoc.jar
OR I need to know how to configure my normal build to download the javadoc/sources withouth the correct .pom file

Gradle to use a jar-with-dependencies in compile task

We have a project that make use of 'jrs-rest-java-client', version: '6.3.1'
The site we used to get the jar from has a certificate issue since September. https://jaspersoft.artifactoryonline.com
We then had to get the jar from a different site.
https://jaspersoft.jfrog.io/
The problem is that a dependency require is missing, but if we use the jar that has "-jar-with-dependencies" it is working. I tried by downloading that jar locally and changing the .gradle to use the local version.
What I would prefer is to have the build to fetch that version directly without having to download first.
How do we specify what jar to use?
dependencies {
compile fileTree(dir: 'lib',
includes: [
'ojdbc8.jar',
])
//compile group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1'
compile group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', USETHISONE: 'jar-with-dependencies'
//compile files("${buildDir}/jrs-rest-java-client-6.3.1-jar-with-dependencies.jar")
}
I have now tried as suggested;
repositories {
mavenCentral()
// to handle broked jasper reports dependencies
maven {
url 'http://jasperreports.sourceforge.net/maven2'
url 'https://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/'
url "https://jaspersoft.jfrog.io/jaspersoft/jaspersoft-clients-releases"
}
}
dependencies {
implementation project(':common:project-common-properties')
implementation project(':common:project-common-mail')
implementation fileTree(dir: 'lib', includes: [
'ojdbc8.jar'
])
implementation group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', classifier: 'jar-with-dependencies'
}
I'm still getting errors at build time...
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all files for configuration ':services:notificationService:compileClasspath'.
> Could not find com.jaspersoft.jasperserver:jasperserver-dto:6.3.0.
Required by:
project :services:notificationService > com.jaspersoft:jrs-rest-java-client:6.3.1
That library is not required if the jrs-rest-java-client-6.3.1-jar-with-dependencies.jar is used.
Thanks all,
The solution was, as seen if the video (Thanks!)
adding a new url:
url "https://jaspersoft.jfrog.io/jaspersoft/jrs-ce-releases"
From the jfrog repo, it shows you how to do this:
compile(group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', classifier: 'jar-with-dependencies')
Add the repo for gradle:
repositories {
jcenter {
name "jaspersoft-releases"
url "https://jaspersoft.jfrog.io/jaspersoft/jaspersoft-clients-releases"
}
}
I'd recommend switching from compile to implementation and using a shorthand to declare the dependency:
implementation "com.jaspersoft:jrs-rest-java-client:6.3.1:jar-with-dependencies"
Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for his life time.
I decided to record a short clip of how I found the appropriate repositories for the artifacts you needed, on jfrog:

IntelliJ no Main Manifest Attribute after creating jar artifact

I am trying to build an jar artefact from this repository. I imported apache commons io and org.json as libraries. When extracting the artifact I find a Manifest file which only contains the information of org.json. You can find the jar here. The manifest file in my Project is not reflected at all. Any help is appreciated. When I run the jar in console with java -jar I get the
Error: No Main Manifest Attribute in XXX.jar.
From the project you provided on github i made the following changes to make it run with java -jar. But first and foremost the project you linked on github does not build with gradle build on a fresh pull.
to make this run you need to add commons io and org.json to you build.gradle file. this makes the build.gradle file look like:
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.json', name: 'json', version: '20190722'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes(
'Main-Class': 'de.bergwacht.esslingen.Main'
)
}
}
your project also has alot of unused dependencies that should be removed that were creating gradle warnings.
in AnweseneheitsTableModel remove the imports:
import com.sun.org.apache.xpath.internal.operations.Bool;
in DienstprotokollInvalidArgumentException remove the imports:
import com.sun.javaws.exceptions.InvalidArgumentException;
in MainForm remove the imports:
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sun.xml.internal.fastinfoset.algorithm.BooleanEncodingAlgorithm;
at this point you can run ./gradlew jar and this will create a jar for you under build/libs
you can run that jar with java -jar build/libs/BWOrgaTool-1.0-SNAPSHOT.jar
that command will run it, it will encounter a NPE:
Exception in thread "main" java.lang.NullPointerException
at de.bergwacht.esslingen.forms.MainForm.<init>(MainForm.java:138)
at de.bergwacht.esslingen.Main.main(Main.java:42)
but thats another problem all together, you can start debugging your program at that point.

Jars needed for JasperReports?

I have downloaded JasperReports jars (jasperreports-5.6.0-project.zip from here) and toke all the jars in the dist folder. These are the files I added to my project:
jasperreports-5.6.0.jar
jasperreports-applet-5.6.0.jar
jasperreports-fonts-5.6.0.jar
jasperreports-javaflow-5.6.0.jar
But when I try my code I always get this error:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
UPDATE 1
I solved that error and I have a lot of more missing jars. Please tell me all the additional jars needed to use JasperReports.
Ok, as I said I added the jars in the dist folder of the downloaded zip (as showed in the question) to my project and to resolve all the problems I had, I added the next jars from the lib folder to my project:
JasperReports 5.6.0 - ...
...poi-3.7-20101029.jar
...commons-beanutils-1.8.0.jar
...commons-collections-3.2.1.jar
...commons-digester-2.1.jar
...commons-javaflow-20060411.jar
...commons-logging-1.1.1.jar
...groovy-all-2.0.1.jar
...iText-2.1.7.js2.jar
...jcommon-1.0.15.jar
...jfreechart-1.0.12.jar
Hope this helps someone!
these are gradle dependencies i needed to make Jasper Report work in Spring Boot:
// https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports
compile group: 'net.sf.jasperreports', name: 'jasperreports', version: '5.0.1'
// https://mvnrepository.com/artifact/commons-logging/commons-logging
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.2'
// https://mvnrepository.com/artifact/com.lowagie/itext
compile group: 'com.lowagie', name: 'itext', version: '2.1.7'
Hope it helps someone.

Categories