[英]Java Sqlite Gradle
I'm pretty new to gradle and java at all.. 我非常喜欢gradle和java ..
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: 我有一个使用sqlite的项目,它通过intellij想法运行良好,但我不能从终端运行它,它抛出一个异常:
java.lang.ClassNotFoundException: org.sqlite.JDBC
I've tried to get process which intellij idea spawns when it runs the project: 我试图在运行项目时获得intellij idea产生的进程:
/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? 正如我在这里看到的想法指定classpath这是解决方案,但如何在不指定classpath的情况下运行项目的.jar文件? I mean can
gradle
generate manifest automatically (because it retrieves sqlite-jdbc from maven org.xerial
repository successfully) and put valid classpath there? 我的意思是可以自动
gradle
生成清单(因为它成功从maven org.xerial
存储库中检索sqlite-jdbc)并将有效的类路径放在那里? 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
. UPD:我不能仅通过
gradle run
命令从终端gradle run
但是我无法通过java -jar myproject.jar
运行该项目。
Here is my build.gradle
: 这是我的
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: 抛出异常的Java源代码:
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: 我通过修改我的
jar
部分,在项目的分发jar文件的类路径中添加sqlite来解决这个问题:
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. 现在gradle下载依赖项并将它们复制到
libs/
目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.