简体   繁体   English

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

[英]Exporting Jar file with Manifest attribute in Android Studio?

I am trying to run a jar from Android studio. 我正在尝试从Android工作室运行一个jar。 After a long Workaround, the jar file run perfectly. 经过长时间的解决方法后,jar文件运行完美。

Now i need to export the jar file from the android studio. 现在我需要从android工作室导出jar文件。 I got the Jar file from the build/libs folder. 我从build / libs文件夹中获取了Jar文件。

But the problem is that the jar file shows a error. 但问题是jar文件显示错误。

no main manifest attribute, in "app.jar" “app.jar”中没有主要的清单属性

So i found this solution. 所以我找到了这个解决方案 Can't execute jar- file: "no main manifest attribute" 无法执行jar文件:“没有主要清单属性”

Then i read about MANIFEST.MF & added the mail class to that file. 然后我读了关于MANIFEST.MF并将邮件类添加到该文件中。

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

After adding those in my gradle. 在我的gradle中添加它们之后。 My MANIFEST.MF contains the MainClass. 我的MANIFEST.MF包含MainClass。

But im still getting the same error? 但是我仍然得到同样的错误? How can i solve this ? 我怎么解决这个问题?

Note: The objective is that I want to Export a Runnable Jar file from the project. 注意:目标是我想从项目中导出Runnable Jar文件。

UPDATE: 更新:

After Adding the MainClass in MANIFEST.MF. 在MANIFEST.MF中添加MainClass之后。 I got stuck with the below error. 我遇到了以下错误。

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 MANIFEST.MF

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

UPDATE 2 更新2

From your solution what i understood is that we need to copy the remoprotocol jar file to the remoserver. 从您的解决方案我明白我们需要将remoprotocol jar文件复制到remoserver。

remoserver project gradle file remoserver项目gradle文件

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
}

After running the gradle task also i am getting the same error. 运行gradle任务后,我也遇到了同样的错误。

First you want to copy all your runtime dependencies into the builddir/libs folder (or a different distribution folder if you so choose). 首先,您要将所有运行时依赖项复制到builddir / libs文件夹(如果您愿意,还可以复制到其他分发文件夹)。 Here is a custom task that would achieve this: 这是一个可以实现此目的的自定义任务:

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

Add your runtime dependency jars as a Class-Path attribute in your manifest file. 将运行时依赖项jar作为Class-Path属性添加到清单文件中。 The jars need to be in the same directory as your runnable jar - which the copy task above achieves. 这些罐子需要和你的runnable jar在同一个目录里 - 上面的复制任务就是这个目录。 (alternatively, you can provide full relative path for your dependency jar location) (或者,您可以为依赖关系jar位置提供完整的相对路径)

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

Some more things to consider: 还有一些事情需要考虑:

  • The application plugin does the same thing; application插件做同样的事情; it adds a task installDist that produces a runnable set of jars along with any dependencies, any readme's, documentation you want to include. 它添加了一个任务installDist ,它生成一组可运行的jar以及任何依赖项,任何自述文件,你想要包含的文档。

  • If you want to produce a single runnable jar without having to bundle dependencies along with it, you should look into creating a "fatjar", for example: 如果你想生成一个可运行的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 } 

I have no experience with Android Studio (or Gradle), but I have with Java. 我没有Android Studio(或Gradle)的经验,但我有Java。 Aren't you trying to set main class instead? 你不是想设置主类吗?

Therefore I suggest changing Class-Path attribute to Main-Class as the manifest should contain Main-Class to be able to invoke something when "running" JAR. 因此,我建议将Class-Path属性更改为Main-Class因为清单应包含Main-Class,以便在“运行”JAR时调用某些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM