使用gradle任务创建后,可执行jar无法找到或加载主类

时间:2016-11-20 21:17:48

标签: gradle gluon-mobile

我使用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运行。有什么问题?

1 个答案:

答案 0 :(得分: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。

相关问题