Java Sqlite Gradle [英] Java Sqlite Gradle

查看:81
本文介绍了Java Sqlite Gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对gradle和java还是很陌生.

I'm pretty new to gradle and java at all..

我有一个使用sqlite的项目,它可以通过intellij的想法很好地运行,但是我不能从终端运行它,这会引发异常:

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

我已经尝试了一个intellij想法在运行项目时产生的过程:

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

正如我在这里看到的,想法指定了解决方案的类路径,但是如何在不指定类路径的情况下运行项目的.jar文件?我的意思是gradle是否可以自动生成清单(因为它成功地从maven org.xerial存储库中检索了sqlite-jdbc)并在其中放置了有效的类路径? 我想从终端成功运行它.

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:我不能仅通过gradle run命令从终端运行它,但不能通过java -jar myproject.jar运行项目.

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.

这是我的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源代码:

Java source code which throws an exception:

try {
    Class.forName("org.sqlite.JDBC");

    this.connection = DriverManager.getConnection(databaseName);
} catch (Exception e) {
    e.printStackTrace();
}

推荐答案

我通过以下方法修改了我的jar部分,在项目的分发jar文件的类路径中添加了sqlite,从而解决了此问题:

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(' ')
    )
}

现在gradle下载依赖项并将其复制到libs/目录.

Now gradle downloads dependencies and copies them to libs/ directory.

这篇关于Java Sqlite Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆