gpt4 book ai didi

java - 将 apache netbeans gradle 项目导出到 jar 文件

转载 作者:行者123 更新时间:2023-12-01 17:17:33 26 4
gpt4 key购买 nike

我创建了一个 Netbeans Gradle 项目,我正在尝试找到将其导出到 .jar 文件的最佳方法,以便可以从 IDE 外部打开它。我在项目中使用依赖项,并且收到 java.lang.NoClassDefFoundError。

为了解决这个问题,我尝试添加

jar {
manifest {
attributes "Main-Class": "CollegeCounselor.Main"
}

from{

configurations.compile.collect{ it.isDirectory() ? it : zipTree(it)}

}
}

到我的 build.gradle 文件。但是,这引发了 java.lang.SecurityException:Manifest 主要属性的签名文件摘要无效。

为了解决这个问题,我发现了一个添加建议

{exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'}

到我的文件。这解决了通过命令行运行文件的问题。现在可以通过 java -jar 运行它。但是,我仍然无法通过 mac 上的 finder 运行它。它给我一个错误,指出 java jar 无法启动,并告诉我检查控制台以获取更多详细信息。我无法理解如何解决该问题,或者为什么我可以通过命令行运行它,但不能通过查找器窗口运行它。我将转到 build/libs/file.jar 路径并尝试打开此文件。

任何有关导出该项目的最佳方式的帮助将不胜感激。

这是我的完整 build.gradle 文件:

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'


mainClassName = 'CollegeCounselor.Main'

repositories {
jcenter()
}



dependencies {
testCompile 'junit:junit:4.12'
compile('com.microsoft.graph:microsoft-graph:1.5.+')
compile group: 'com.microsoft.azure', name: 'msal4j', version: '1.4.0'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
}

jar {
manifest {
attributes "Main-Class": "CollegeCounselor.Main"
}

from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } }
{
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}

}

最佳答案

这对我有用:

  • 使用项目向导文件 > 新建项目... > Java with Gradle > Java 应用程序创建一个名为 gradleproject2 的简单 Gradle 应用程序。
  • 编辑build.gradle based on this SO answer ,添加以下条目:
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'hello.HelloWorld'
)
}
}
  • 然后将 hello.HelloWorld 替换为您的包和包含 main() 的类的名称,在我的例子中是 gradleproject2.Main ,所以我修改后的 build.gradle 看起来像这样:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'


mainClassName = 'gradleproject2.Main'

repositories {
jcenter()
}

dependencies {
testImplementation 'junit:junit:4.13'
}

jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'gradleproject2.Main'
)
}
}
  • 构建您的项目,将 jar 文件 gradleproject2.jar 放入 {project directory}\build\libs 中。

build.gradle 的更改的效果是将 jar 文件中 MANIFEST.MF 的内容更改为:

Manifest-Version: 1.0

...对此:

Manifest-Version: 1.0
Class-Path:
Main-Class: gradleproject2.Main

最后,从命令行运行 jar 文件。就我而言,命令很简单 java -jar gradleproject2.jar:

runJar

关于java - 将 apache netbeans gradle 项目导出到 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61352469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com