代码之家  ›  专栏  ›  技术社区  ›  Chris

通过jpackage打包无法找到或加载主类

  •  1
  • Chris  · 技术社区  · 3 年前

    使用 jpackage 我找不到电话 pkg 在macOS上运行。安装按预期进行,但当我启动安装的应用程序时,它会启动,然后立即停止。

    试图通过CLI启动它,它会抛出

    Error: Could not find or load main class com.example.Application
    Caused by: java.lang.ClassNotFoundException: com.example.Application
    

    我通过了考试 MainClass 作为论据 com.example.Application ,我可以在下面安装的捆绑包中看到它 Contents/app/example.jar

    使用Gradle插件 id "org.panteleyev.jpackageplugin" version "1.3.1" 要构建本机安装程序,请执行以下操作:

    def os = org.gradle.internal.os.OperatingSystem.current()
    def pkgType = os.windows ? 'msi' : os.linux ? 'deb' : 'pkg'
    def inputDir = "$buildDir/input"
    
    task copyDependencies (type: Copy) {
        from configurations.runtimeClasspath
        into inputDir
    }
    
    task copyJar (type: Copy) {
        from tasks.jar
        into inputDir
    }
    
    jpackage {
        dependsOn clean
        dependsOn bootJar
        dependsOn copyDependencies
        dependsOn copyJar
    
        type = pkgType
    
        input = inputDir
        destination = "$buildDir/dist"
    
        appName = 'Example'
        vendor = 'com.example'
    
        mainJar = tasks.jar.getArchiveFileName().get()
        mainClass = 'com.example.Application'
    
        javaOptions = ['-Dfile.encoding=UTF-8']
    }
    

    当通过IntelliJ/CLI启动时,jar运行正常。

    我还需要在这里做什么?

    0 回复  |  直到 3 年前
        1
  •  0
  •   Chris    3 年前

    因为我正在尝试运行Springboot应用程序,所以诀窍是不要像通常那样尝试启动主类,而是使用 org.springframework.boot.loader.JarLauncher

    这在一篇文章中得到了更恰当的解释 blog post 春天队

    因此,Springboot的完整工作示例如下所示:

    plugins {
        id 'org.springframework.boot' version '2.6.0-SNAPSHOT'
        id 'io.spring.dependency-management' version '1.0.11.RELEASE'
        id 'org.panteleyev.jpackageplugin' version '1.3.1'
    
        id 'application'
    }
    
    group = 'space.forloop'
    version = '1.0.6'
    
    compileJava.options.encoding = 'UTF-8'
    compileTestJava.options.encoding = 'UTF-8'
    javadoc.options.encoding = 'UTF-8'
    
    
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
        maven { url 'https://repo.spring.io/snapshot' }
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
    }
    
    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }
    
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }
    
    application {
        mainClass = 'com.example.Application'
        applicationDefaultJvmArgs = ["-Dfile.encoding=UTF-8"]
    }
    
    bootJar {
        manifest {
            attributes 'Implementation-Version': "${project.version}"
            attributes 'Implementation-Title': "${project.name}"
        }
    }
    
    // Not required but useful if you want to configure a little more.
    def os = org.gradle.internal.os.OperatingSystem.current()
    def pkgType = os.windows ? 'msi' : os.linux ? 'deb' : 'pkg'
    
    jpackage {
        dependsOn "bootJar"
    
        type = pkgType
    
        input = "${buildDir}/libs"
        destination = "${buildDir}/dist"
    
        appName = 'Example'
        vendor = 'com.example'
    
        mainJar = bootJar.archiveFileName.get()
        mainClass = "org.springframework.boot.loader.JarLauncher"
    
        javaOptions = ['-Dfile.encoding=UTF-8']
    
        macPackageName = bootJar.archiveBaseName.get()
    }