我是新手,现在就学习。我正在尝试使用gradle构建一个可运行的jar(我的构建jar应该从命令提示符运行)。我刚开始使用一个带有依赖项的简单HelloWorld
项目,并尝试使用gradle构建它。我不是在使用maven。所以我将依赖jar添加到项目文件夹中的lib文件夹中。以下是详细信息。
Gradle版本:3.1 Eclipse版本:Neon
HelloWorld.java
package com.padhu.test;
import org.joda.time.LocalTime;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
LocalTime currentTime = new LocalTime();
System.out.println("The current local time is: " + currentTime);
System.out.println("Hello World");
}
}
项目结构:
build_path_snap
build.gradle文件:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'com.padhu.test.HelloWorld'
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
compile files(fileTree(dir: 'lib', includes: ['*.jar']))
}
jar {
manifest {
attributes(
'Main-Class': 'com.padhu.test.HelloWorld'
)
}
}
我可以在我的cmd中成功执行gradle build
并生成jar文件。但在jar文件中,我只能看到HelloWorld文件夹结构,但无法看到依赖文件。所以我无法从命令提示符运行jar,因为我得到NoClassDefFoundError: org/joda/time/LocalTime
..
我尝试通过搜索和阅读博客来解决问题。但我无法成功运行jar。
请帮帮我。
答案 0 :(得分:0)
看来,您误解了SELECT itm.Id ,
ISNULL(itm.Debit, 0) AS Debit ,
ISNULL(itm.Credit, 0) AS Credit ,
itm.State ,
itm.DocCreateDate ,
ISNULL(itm.Num, 0) AS Num ,
ISNULL(itm.DocTypeRef, 0) AS DocTypeRef ,
itm.Year ,
itm.Month ,
ISNULL(itm.DebitCount, 0) AS DebitCount ,
ISNULL(itm.CreditCount, 0) AS CreditCount ,
itm.DL ,
itm.DL2 ,
itm.DL3 ,
itm.DL4 ,
itm.DL5 ,
itm.DL6 ,
itm.DL7 ,
ISNULL(itm.FCRef, 0) AS FCRef ,
itm.FollowUpNum ,
ISNULL(itm.BranchRef, 1) AS BranchRef ,
itm.DocHeaderRef ,
ISNULL(itm.RowNum, 0) AS RowNum ,
ISNULL(itm.DailyNum, 0) AS DailyNum ,
ISNULL(itm.TempNum, 0) AS TempNum ,
ISNULL(itm.RefNum, 0) AS RefNum ,
itm.Descript ,
itm.Count ,
itm.FollowUpDate ,
itm.FCVal ,
itm.FCRateVal ,
itm.FactorNum ,
ISNULL(itm.DebitFCVal, 0) AS DebitFCVal ,
ISNULL(itm.CreditFCVal, 0) AS CreditFCVal ,
sl.Id AS SLRef ,
sl.SLCode ,
sl.Title AS SLTitle ,
CASE WHEN ISNULL(sl.DLSRef, 0) > 0 THEN 1
ELSE 0
END AS HasDL ,
CASE WHEN ISNULL(sl.DLSRef2, 0) > 0 THEN 1
ELSE 0
END AS HasDL2 ,
CASE WHEN ISNULL(sl.DLSRef3, 0) > 0 THEN 1
ELSE 0
END AS HasDL3 ,
CASE WHEN ISNULL(sl.DLSRef4, 0) > 0 THEN 1
ELSE 0
END AS HasDL4 ,
CASE WHEN ISNULL(sl.DLSRef5, 0) > 0 THEN 1
ELSE 0
END AS HasDL5 ,
CASE WHEN ISNULL(sl.DLSRef6, 0) > 0 THEN 1
ELSE 0
END AS HasDL6 ,
CASE WHEN ISNULL(sl.DLSRef7, 0) > 0 THEN 1
ELSE 0
END AS HasDL7 ,
CASE WHEN ISNULL(sl.HasFC, 0) > 0 THEN 1
ELSE 0
END AS HasFC ,
1 AS HasFollow ,
tl.Id AS TLRef ,
tl.Title AS TLTitle ,
tl.TLCode ,
gl.Id AS GLRef ,
gl.Title AS GLTitle ,
gl.GLCode ,
gl.Balance AS GLBalance
FROM Acc.DocItem AS itm
LEFT OUTER JOIN Acc.SL AS sl ON itm.SLRef = sl.Id
LEFT OUTER JOIN Acc.TL AS tl ON sl.TLRef = tl.Id
LEFT OUTER JOIN Acc.GL AS gl ON tl.GLRef = gl.Id
WHERE ( itm.SLRef > 0 )
插件的行为方式。它不会创建一个所谓的fat-jar,它包含所有它的依赖项,而是创建一个jar并能够运行它,将所有依赖项添加到类路径中。
您可以按如下方式运行您的应用程序:
application
或者您可以使用自动应用的分发插件,并使用脚本创建项目分发,以启动它,并将所有依赖库分别放在单独的文件夹中。只需运行它:
gradle run
您可以在official user guide中了解更多相关信息。