简体   繁体   English

使用 Gradle 和 NetBeans 构建可执行文件 jar

[英]Building executable jar with Gradle and NetBeans

I have searching to solve this problem for two days and I did not find solution.我已经搜索了两天来解决这个问题,但我没有找到解决方案。 Firstly I wanted to build fat-jar, then I gave up on that it looked more complicated so I started building normal executable jar.首先我想构建 fat-jar,然后我放弃了它看起来更复杂,所以我开始构建普通的可执行文件 jar。 I am building some program inside NetBeans 8.2 with Gradle plugin( http://plugins.netbeans.org/plugin/44510/gradle-support )我正在使用 Gradle 插件在 NetBeans 8.2 中构建一些程序( http://plugins.netbeans.org/plugin/44510/gradle-support

My project structure is like this:我的项目结构是这样的:

Project Structure项目结构

I am relatively new to Gradle I am using it less then one month.我对 Gradle 比较陌生,我使用它不到一个月。 My build.gradle looks like this:我的build.gradle看起来像这样:

repositories {
   mavenCentral()
}

dependencies {
    // TODO: Add dependencies here ..
//    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
//    testCompile group: 'junit', name: 'junit', version: '4.10'

    // https://mvnrepository.com/artifact/org.apache.poi/poi
    compile group: 'org.apache.poi', name: 'poi', version: '3.16'

    // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.16'

    // https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.6'


}

apply plugin: 'java'


mainClassName = "paket.Glavna"

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes("Main-Class": "Glavna" )
    }
}

Now, I cant get it working to make executable jar.现在,我无法让它运行可执行 jar。 I think i have traced problem that there isnt path to main class in jar but I dont know why it isnt adding it.我想我已经追踪到在 jar 中没有通往主要 class 的路径的问题,但我不知道为什么它没有添加它。 Error i get is:我得到的错误是:

Error: Could not find or load main class DnevniIzvestajG-1.0.jar错误:无法找到或加载主 class DnevniIzvestajG-1.0.jar

jar runs with (inside Glavna class is main): java -cp projectname.jar paket.Glavna jar 运行(在 Glavna 内部 class 是主要的): java -cp projectname.Z68995Z48443249DACD4848404Glavna1

Also I have tried running manifest-addition but taht also didnt add link to main class.我也尝试过运行清单添加,但也没有添加到主要 class 的链接。

Try adding a package to the class with main() method. 尝试使用main()方法将包添加到类中。

jar {
    archiveName = 'Glavna.jar'

    manifest {attributes 'Main-Class': 'paket.Glavna'}

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

Building a fat jar like this is not preferable, try the Application plugin 像这样构建一个胖子罐不是可取的,请尝试使用Application插件

I use below way to add all dependencies in.jar of gradle project.我使用以下方式在 gradle 项目的 jar 中添加所有依赖项。

build.gradle :- build.gradle :-

jar {
    manifest {
        attributes(
                'Main-Class': 'com.main.MainClass'
        )
    }

 duplicatesStrategy = DuplicatesStrategy.INCLUDE
 from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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