Java Sqlite Gradle - java

I'm pretty new to gradle and java at all..
I have a project which uses sqlite and it runs fine through intellij idea but I can't run it from the terminal, it throws an exception:
java.lang.ClassNotFoundException: org.sqlite.JDBC
I've tried to get process which intellij idea spawns when it runs the project:
/usr/lib/jvm/java-7-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=42986 -Dfile.encoding=UTF-8 -cp /home/user/my_project/target/classes/main:/home/user/my_project/target/resources/main:/home/user/.gradle/caches/modules-2/files-2.1/org.xerial/sqlite-jdbc/3.7.2/7a3d67f00508d3881650579f7f228c61bfc1b196/sqlite-jdbc-3.7.2.jar Main
As I see here idea specifies classpath which is the solution but how can I run my .jar file of the project without specifying classpath? I mean can gradle generate manifest automatically (because it retrieves sqlite-jdbc from maven org.xerial repository successfully) and put valid classpath there?
I want to run it successffully from the terminal.
UPD: I can't it run from the terminal only by gradle run command but I can't run the project by java -jar myproject.jar.
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'application'
group = 'myproject'
version = '0.3'
description = """my project description"""
buildDir = "target"
mainClassName = "Main"
repositories {
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
testCompile group: 'junit', name: 'junit', version:'3.8.1'
compile 'org.xerial:sqlite-jdbc:3.7.2'
}
jar {
manifest.attributes("Main-Class": "Main")
}
/* Overwriting distribution tasks: */
task distZip(type:Zip, overwrite:true) {
archiveName = "$project.name-$version" + '.zip'
from "target/libs"
}
task distTar(type:Tar, overwrite:true) {
archiveName = "$project.name-$version" + '.tar'
from "target/libs"
}
Java source code which throws an exception:
try {
Class.forName("org.sqlite.JDBC");
this.connection = DriverManager.getConnection(databaseName);
} catch (Exception e) {
e.printStackTrace();
}

I've fixed this issue by adding sqlite in the classpath of distribution jar file of the project by modifying my jar section by this:
task copyDependenciesToTarget(type: Copy) {
println 'Copying dependencies to target...'
configurations.compile.collect().each { compileDependency ->
copy {
with from (compileDependency.getPath()) {
include '*'
}
into 'target/libs/libs'
}
}
}
build.dependsOn(copyDependenciesToTarget)
jar {
manifest.attributes(
"Main-Class": "Main",
"Class-Path": configurations.compile.collect { 'libs/' + it.getName()}.join(' ')
)
}
Now gradle downloads dependencies and copies them to libs/ directory.

Related

Error: impossible to find or load main class when running jar file on windows

I am working on a java project using gradle for a homework assignment and I got troubles running JAR files built off gradle via the windows command prompt :
image
Here's my build.gradle txt :
apply plugin: 'java'
apply plugin: 'antlr'
repositories {
mavenCentral()
}
dependencies {
antlr 'org.antlr:antlr4:4.7.1'
testCompile 'junit:junit:4.12'
}
generateGrammarSource {
arguments += ['-no-visitor', '-no-listener']
outputDirectory = new File(buildDir.toString() + "/generated-src/antlr/main/TP2/")
}
jar {
manifest {
attributes (
'Main-Class': 'TP2.Main',
'Class-Path': configurations.runtimeClasspath.files.join(' ')
)
}
}
I've already checked my classpath directory, class name... When I type the next command : jar -tf build\libs\TP2.jar I can see my class well packed in my jar and my class is at this directory : build\classes\java\main\TP2\Main.class
So I don't see why it is not working.
I'm not an expert with gradle that's why any help would be really appreciated but I think the problem propably come from the manifest.

Building gradle plugin

I'm developing a gradle plugin alongside with a project:
plugin_consumer
|
+ build.gradle
+ src/
plugin
|
+ build.gradle
+ libs/
+ src/
All jars are moved to plugin/libs after built, and plugin consumer module uses plugin like so:
buildscript {
repositories {
// ...
}
dependencies {
classpath fileTree(dir:'../plugin/libs', include: ['*.jar'])
}
}
apply plugin: 'my.custom.plugin'
I tested and this setup worked fine, however the plugin .jar is being built without dependencies, and as soon as I add some dependencies to the project the consumer buildscript started crashing because of NoClassDefFoundError exceptions. The problem is when I build a fat jar instead the script no longer finds plugin:
// from plugin/build.gradle
// Configurations to build jar with dependencies
configurations {
jar.archiveName = 'plugin.jar'
}
jar {
archiveName = 'plugin.jar'
baseName = project.name + '-all'
from { configurations.compile.collect {
println it
it.isDirectory() ? it : zipTree(it)
} }
}
Running commands on plugin_consumer project throws error:
> Failed to apply plugin [id 'my.custom.plugin']
> Plugin with id 'my.custom.plugin' not found.
I haven't identified the problem, but have found a solution. Somehow the jar instruction to package all of the sources have messed up the plugin jar file. The following configurations have worked on plugin side instead:
//...
jar {
from {
configurations.compileWithDependencies.collect { it.isDirectory() ? it : zipTree(it) }
}
}
configurations {
compileWithDependencies
}
dependencies {
compile gradleApi()
compileWithDependencies 'my.dependency1'
compileWithDependencies 'my.dependency1'
testCompile 'test.dependency'
configurations.compile.extendsFrom(configurations.compileWithDependencies)
}
From the looks of it I would guess that gradleApi() dependencies should not be compiled into the plugin jar.

"Could not find or load main class" error for Gradle-generated Scala JAR

After much research, I can't seem to get to the root of a problem I am having in generating a runnable Scala jar file using Gradle. I'm overriding the 'jar' Gradle task to create a jar file (dependencies included) that executes starting from my main class file. However, whenever I run it, regardless of what I use for a Main-Class attribute, the console throws a "Could not find or load main class" error. Here's what I have so far:
build.gradle
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'java'
apply plugin: 'scala'
apply plugin: 'application'
repositories {
mavenCentral()
// some other repos
}
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = "com.test.Main"
dependencies {
// my dependencies
}
jar {
zip64 = true
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
src/main/scala/com/test/Main.scala
package com.test
object Main {
def main(args: Array[String]): Unit = {
print("Hello world");
}
}
In fact, when I run "java tf test.jar", it shows "com/test/Main.class" in the root of the jar! Am I missing some important class path info or something? I'm running Java 1.8 on macOS Sierra using Gradle 3.5. Any help is appreciated. Thanks!
I had a similar problem and it turns out that my META-INF folder inside my jar file contains a few RSA, SF, and DSA files.
Once I excluded them, it worked!
here is how to based on your jar declaration
jar {
zip64 = true
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it)
}
exclude ('META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA')
}
You can use the Shadow Jar Plugin instead of your own jar definition.
Benefits of Shadow
Shadowing a project output has 2 major use cases:
Creating an executable JAR distribution
Bundling and relocating common dependencies in libraries to avoid classpath conflicts
Basic setup:
shadowJar {
baseName = 'your-app'
classifier = 'all'
version = version
configurations = [project.configurations.compile]
}
jar {
manifest {
attributes 'Main-Class': 'com.test.Main'
}
}
You can use the new syntax of Gradle Plugins:
plugins {
id 'java'
id 'scala'
id 'application'
}

Running Flyway migrations on gradle refresh

I am still trying to wrap my head around gradle so please bear with me.
I'm trying to set up flyway migrations to run automatically on every gradle refresh/build instead of having to run a command-line command. So when someone is pulling the latest changes including some new migrations their local db will be up to date.
I have this build.grade in the root dir:
buildscript {
...
repositories {
mavenLocal()
mavenCentral()
}
allprojects {
...
}
project(":core") {
apply plugin: "java"
dependencies {
compile project(":shared")
}
}
project(":server") {
apply plugin: "java"
dependencies {
compile project(":shared")
}
}
project(":shared") {
apply plugin: "java"
}
tasks.eclipse.doLast {
delete ".project"
}
Where I've tried merging the lines from the flyway get started pages.
I basically want to run the $ gradle flywayMigrate -i command from within the build.gradle file. I've tried with build.finalizedBy(flywayMigrate) but to no avail.
How would I do something like that?
Thanks in advance!
In order to make build finalized by flywayMigrate you need to run gradle build which probably You didn't.
To run flywayMigrate you can use defaultTasks which will run the tasks configured if no other are provided. So:
buildscript {
dependencies {
classpath 'com.h2database:h2:1.4.191'
}
}
plugins {
id "org.flywaydb.flyway" version "4.1.2"
}
flyway {
url = 'jdbc:h2:file:./target/foobar'
user = 'sa'
}
defaultTasks 'flywayMigrate'
When it comes to configuring -i (logging level) from within build.gradle it seems to be impossible with the newest gradle versions.

Gradle Build Output Jar Doesn't Run

First off, this might be a dupe of Gradle build successful, error running jar
I've build a very basic Dropwizard project that is built with Gradle. My build.gradle file looks something like this:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
// use Java 1.8 by default
sourceCompatibility = 1.8
targetCompatibility = 1.8
// default to UTF-8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
// application version and name
mainClassName='com.mydomain.WebServiceApplication'
version = '0.1-' + (System.getenv("SVN_REVISION") == null ? "0" : System.getenv("SVN_REVISION"))
jar {
manifest {
attributes 'Implementation-Title': 'WebService',
'Implementation-Version': version,
'Main-Class': mainClassName
}
}
// project variables go here
project.ext {
dropwizardVersion='0.9.1'
}
// repositories that we're getting libraries from
repositories {
mavenCentral()
}
dependencies {
// compile-time dependencies
compile (
'commons-collections:commons-collections:3.2',
'io.dropwizard:dropwizard-core:' + dropwizardVersion,
'io.dropwizard:dropwizard-client:' + dropwizardVersion,
'org.logback-extensions:logback-ext-loggly:0.1.4',
'ch.qos.logback.contrib:logback-json-classic:0.1.2',
'ch.qos.logback.contrib:logback-jackson:0.1.2'
)
// test dependencies - these will be stripped from production jar
testCompile (
'io.dropwizard:dropwizard-testing:' + dropwizardVersion,
'junit:junit:4.+'
)
}
run {
args 'server', './src/main/resources/config.yml'
}
When I run gradle build, a ./build/libs/ folder is created that contains a jar called webservice-0.1.jar. If I try to execute this jar with java -jar webservice-0.1.jar, I get the error Error: Could not find or load main class com.mydomain.WebServiceApplication.
Thing is, that file most certainly exists. I can do the following:
$ makedir tmp
$ cd tmp
$ unzip ../webservice-0.1.jar
$ ls ./com/mydomain/
and the file WebServiceApplication.class is there. Is there some kind of classpath thing that I have to do in order to get java to find classes inside of my jar? I'm lost here.

Categories