首页 > 解决方案 > 执行Gradle编译的jar,导致“没有主类”

问题描述

gradle 的版本是 6.3-all。这是我的 build.gradle:

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}

group 'com.dennis'
version '1.0-SNAPSHOT'

repositories {
    mavenLocal()
    mavenCentral()
}

jar {
    from {
        configurations.runtime.collect { zipTree(it) }
    }
    manifest {
        attributes 'Main-Class': 'com.dennis.tcp.robot.RobotClient'
    }
}

dependencies {
    /*...*/
}


compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

这里是 settings.gradle

rootProject.name = 'robot'

这是主要课程:

package com.dennis.tcp.robot;

/*import ignored*/

public class RobotClient {

    private static final String URL = "192.168.10.140";
    private static final int PORT = 9760;

    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE,true)
                    .handler(new RobotClientInitializer());

            ChannelFuture future = bootstrap.connect(URL, PORT).sync();
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}

打包进度很顺利,但是输出的 jar 无法执行,并给出no main class jar 文件的异常显示主类 RobotClient 确实已编译。但是,java -jar无法执行或找到主类。为什么会这样?

标签: gradlejar

解决方案


推荐阅读