【问题标题】:Executable jar can't find or load main class after it's created with a gradle task可执行 jar 在使用 gradle 任务创建后无法找到或加载主类
【发布时间】:2017-04-04 05:13:22
【问题描述】:

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

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

这是未改动的 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.**.*'
        ]
    }
}

和“主类”:

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")));
    }
}

确实,类中没有main 方法,但您不应该需要一个。它直接从 IDE 运行良好。有什么问题?

【问题讨论】:

    标签: gradle gluon-mobile


    【解决方案1】:

    当您在 Gluon 项目上执行 jar 任务时,您将获得只是一个 jar,其中包含项目的主包和桌面包下的类和资源。在你的情况下,是这样的:

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

    如果你想创建一个可执行的 jar,你需要创建一个 shadowJarfatJar,将所有的依赖捆绑在那个 jar 中。

    你可以为它添加这个插件:com.github.johnrengelman.shadow,基于这个project

    添加插件后,您需要做的就是让它知道您的自定义配置,因为它不知道桌面配置。

    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]
    }
    

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

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

    【讨论】:

    • 酷。你能帮我“添加插件”吗?我要签出项目并构建一个 jar,然后包含它吗?
    • 我刚刚...我发布的代码已经包含了您需要做的所有事情:添加插件只需要引用依赖项并使用apply plugin...。 Gradle 为您管理。
    • 啊,太好了!谢谢。如果有人仍然会在build 下查找,我还将补充说构建影子 jar 的 gradle 任务在projectName/shadow/shadowJar 下。
    • 好的,我已经用任务菜单的屏幕截图编辑了答案。如果您有多个 shadow* 任务,则会出现 shadow 子菜单。
    猜你喜欢
    • 2018-11-17
    • 2017-12-14
    • 2014-09-04
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多