首页 文章

在Android Studio中导出具有Manifest属性的Jar文件?

提问于
浏览
4

我正在尝试从Android工作室运行一个jar . 经过长时间的解决方法后,jar文件运行完美 .

现在我需要从android工作室导出jar文件 . 我从build / libs文件夹中获取了Jar文件 .

但问题是jar文件显示错误 .

“app.jar”中没有主要清单属性

所以我找到了这个解决方案Can't execute jar- file: "no main manifest attribute"

然后我读了关于MANIFEST.MF并将邮件类添加到该文件 .

jar {
    manifest.attributes(
            'Main-Class': "com.Remo.server.RemoServerApp"
    )
}

在我的gradle中添加它们之后 . 我的MANIFEST.MF包含MainClass .

但是我仍然得到同样的错误?我怎么解决这个问题?

Note: 目标是我想从项目中导出Runnable Jar文件 .

UPDATE:

在MANIFEST.MF中添加MainClass之后 . 我遇到了以下错误 .

Exception in thread "main" java.lang.NoClassDefFoundError: com/Remo/protocol/RemoConnection
    at com.Remo.server.RemoServerApp.<init>(RemoServerApp.java:33)
    at com.Remo.server.RemoServerApp.main(RemoServerApp.java:97)
Caused by: java.lang.ClassNotFoundException: com.Remo.protocol.RemoConnection
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

MANIFEST.MF

清单 - 版本:1.0
Main-Class:com.Remo.server.RemoServerApp

UPDATE 2

从您的解决方案我明白我们需要将remoprotocol jar文件复制到remoserver .

remoserver project gradle file

apply plugin: 'java'

sourceSets {
    main {
        resources.srcDirs = ['src/main/resources']
    }
}

dependencies {
    compile project(':remoprotocol')
    compile files('libs/bluecove-2.1.1.jar')
}

jar {
    manifest.attributes(
            'Main-Class': "com.remo.server.remoServerApp"
    )
    manifest.attributes(
            'Class-Path': configurations.runtime.files.collect { it.getName() }.join(' '))
}

task copyRTDependenciesToLib(type: Copy) {
    into "$buildDir/output/lib"
    from configurations.runtime
}

运行gradle任务后,我也遇到了同样的错误 .

2 回答

  • -1

    首先,您要将所有运行时依赖项复制到builddir / libs文件夹(如果您愿意,还可以复制到其他分发文件夹) . 这是一个可以实现此目的的自定义任务:

    task copyRTDependenciesToLib(type: Copy) {
        into "$buildDir/libs"
        from configurations.runtime
    }
    

    将运行时依赖项jar作为Class-Path属性添加到清单文件中 . 这些 jar 需要和你的runnable jar在同一个目录里 - 上面的复制任务就是这个目录 . (或者,您可以为依赖关系jar位置提供完整的相对路径)

    jar {
        manifest {
            attributes(
               "Main-Class": "com.Remo.server.RemoServerApp",
               "Class-Path": configurations.runtime.files.collect { it.getName() }.join(' '))
             )
        }
    }
    

    还有一些事情需要考虑:

    • application插件做同样的事情;它添加了一个任务 installDist ,它生成一组可运行的jar以及任何依赖项,任何自述文件,你想要包含的文档 .

    • 如果你想生成一个可运行的jar而不必同时捆绑依赖项,你应该考虑创建一个“fatjar”,例如:

    task fatJar(type: Jar) {
        manifest {
            attributes 'Implementation-Title': 'Gradle Jar File Example',  
                'Implementation-Version': version,
                'Main-Class': "com.Remo.server.RemoServerApp"
        }
        baseName = project.name
    
        //collect all dependencies
        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
        with jar
    }
    
  • 4

    我没有Android Studio(或Gradle)的经验,但我有Java . 你不是想设置主类吗?

    因此,我建议将 Class-Path 属性更改为 Main-Class ,因为清单应包含Main-Class,以便能够在"running" JAR时调用某些内容 .

相关问题