简体   繁体   中英

Java Sqlite Gradle

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM