使用gradle任务创建后,可执行jar无法找到或加载主类 [英] Executable jar can't find or load main class after it's created with a gradle task

查看:1256
本文介绍了使用gradle任务创建后,可执行jar无法找到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用胶子插件来创建一个新的单个视图项目,并且在其文件中没有任何改变。如果我使用gradle任务 application>运行它工作正常。但是如果我使用taks build> jar 它在 SingleViewProject \build\libs 下创建了jar,当我从命令行运行它时,我得到了


错误:无法找到或加载主类com.gluonapplication.GluonApplication





$ b

  buildscript {
repositories {
jcenter()
}
依赖关系{
classpath'org.javafxports:jfxmobile-plugin:1.1.1'
}
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
jcenter()
maven {
url'http://nexus.gluonhq.com/nexus/content/存储库/版本'
}
}

mainClassName ='com.gluonapplication.GluonApplication'

dependencies {
compile'c​​om.gluonhq :charm:4.1.0'
}

jfxmobile {
downConfig {
version ='3.0.0'
pl ugins'display','lifecycle','statusbar','storage'
}
android {
manifest ='src / android / AndroidManifest.xml'
}
ios {
infoPList = file('src / ios / Default-Info.plist')
forceLinkClasses = [
'com.gluonhq。**。*',
' javax.annotations。**。*',
'javax.inject。**。',
'javax.json。**。*',
'org.glassfish.json 。**。*'
]
}
}

和主类:

  package com.gluonapplication; 

import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.visual.Swatch;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

公共类GluonApplication扩展了MobileApplication {

public static final String BASIC_VIEW = HOME_VIEW;
$ b $ @Override
public void init(){
addViewFactory(BASIC_VIEW,() - > new BasicView(BASIC_VIEW));
}

@Override
public void postInit(Scene scene){
Swatch.BLUE.assignTo(scene);
$ b $((Stage)scene.getWindow())。getIcons()。add(new Image(GluonApplication.class.getResourceAsStream(/ icon.png)));


确实没有 main 方法,但你不需要。它直接从IDE运行。问题是什么?

解决方案

当您对 jar Gluon项目中,您将获得一个包含项目主包和桌面包下的类和资源的jar包。在你的情况下,像这样:





该jar包含主类,但它不包含任何依赖项(Gluon Charm)。



<如果你想创建一个可执行的jar文件,你需要创建一个 shadowJar fatJar ,它捆绑所有你可以为它添加这个插件: com.github.johnrengelman.shadow ,基于该jar包的依赖项。



在这个项目上。

一旦你添加这个插件,你只需要知道你的自定义配置,因为它不知道桌面就是这样。

  buildscript {
存储库{
jcenter()
}
依赖关系{
classpath'org.javafxports:jfxmobile-plugin:1.1.1'
classpath'com.github.jengelman.g radle.plugins:shadow:1.2.4'
}
}

apply plugin:'org.javafxports.jfxmobile'
apply plugin:'com.github。 johnrengelman.shadow'

...

//将桌面依赖关系添加到任务
shadowJar {
configurations = [project.configurations.desktopRuntime]

重新载入您的项目并执行 shadowJar 。您可以在任务菜单下找到它:





您会在/builds/libs/yourProjectName-all.jar下找到您的可执行文件jar。



< img src =https://i.stack.imgur.com/NFTUV.pngwidth =300>


I used the gluon plugin to create a new single view project and changed nothing in its files. If I use the gradle task application > run it works fine. But if I use the taks build > jar it created the jar under SingleViewProject\build\libs and when I run it from the command line i get

Error: Could not find or load main class com.gluonapplication.GluonApplication

This is the untouched build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.1.1'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.gluonapplication.GluonApplication'

dependencies {
    compile 'com.gluonhq:charm:4.1.0'
}

jfxmobile {
    downConfig {
        version = '3.0.0'
        plugins 'display', 'lifecycle', 'statusbar', 'storage'
    }
    android {
        manifest = 'src/android/AndroidManifest.xml'
    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = [
                'com.gluonhq.**.*',
                'javax.annotations.**.*',
                'javax.inject.**.*',
                'javax.json.**.*',
                'org.glassfish.json.**.*'
        ]
    }
}

and the "main class":

package com.gluonapplication;

import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.visual.Swatch;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class GluonApplication extends MobileApplication {

    public static final String BASIC_VIEW = HOME_VIEW;

    @Override
    public void init() {
        addViewFactory(BASIC_VIEW, () -> new BasicView(BASIC_VIEW));
    }

    @Override
    public void postInit(Scene scene) {
        Swatch.BLUE.assignTo(scene);

        ((Stage) scene.getWindow()).getIcons().add(new Image(GluonApplication.class.getResourceAsStream("/icon.png")));
    }
}

It's true that there is no main method in the class but you shouldn't need one. And it runs fine straight from the IDE. What's the problem?

解决方案

When you execute the jar task on a Gluon project, you will get just a jar with the classes and resources under the main and desktop packages of your project. In your case, something like this:

That jar contains the main class, but it doesn't contain any of the dependencies (Gluon Charm).

If you want to create an executable jar, you need to create a shadowJar or fatJar, that bundles all the dependencies within that jar.

You can add this plugin for it: com.github.johnrengelman.shadow, based on this project.

Once you add the plugin, all you need to do is let it know about your custom configurations, as it doesn't know about the Desktop one.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.1.1'
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
    }
}

apply plugin: 'org.javafxports.jfxmobile'
apply plugin: 'com.github.johnrengelman.shadow'

...

// Add desktop dependencies to the task
shadowJar {
    configurations = [project.configurations.desktopRuntime]
}

Reload your project and execute shadowJar. You can find it under the Tasks menu:

You will find your executable jar under /builds/libs/yourProjectName-all.jar.

这篇关于使用gradle任务创建后,可执行jar无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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