简体   繁体   English

分发 Java 应用程序

[英]Distributing java application

I have recently developed some java applications that i want others to run on their machines.我最近开发了一些我希望其他人在他们的机器上运行的 Java 应用程序。 I did some research and now know that to distribute java code you need to create .jar file.我做了一些研究,现在知道要分发 Java 代码,您需要创建 .jar 文件。 Well i did that, but when I am distributed those file it runs on some computers but on others it return an error saying: "Main class could not found".好吧,我做到了,但是当我分发这些文件时,它会在某些计算机上运行,​​但在其他计算机上它会返回错误消息:“找不到主类”。

  1. Is there any problem with JRE version. JRE 版本有没有问题。
  2. How would a user know that on which version he/she should run the application.用户如何知道他/她应该在哪个版本上运行应用程序。
  3. can i package the correct jre version with my app/jar file.我可以用我的 app/jar 文件打包正确的 jre 版本吗? how??如何??
  4. Are jar files not compatible with other version of jre except in which they are compiled. jar 文件是否与其他版本的 jre 不兼容,除非它们被编译。

Option1: Create a manifest file with entry as below and include in the jar: 选项1:使用如下条目创建清单文件并包含在jar中:

     Main-Class: MainProgram

Option2: Just mention your Main class name while running the program eg if you jar name is myprogram.jar and main class is MainProgram then run the program as below: 选项2:在运行程序时只需提及您的Main类名称,例如,如果jar名称是myprogram.jar ,主类是MainProgram则运行程序如下:

        java -cp myprogram.jar MainProgram

If your MainProgram takes some arguments, them pass them as well in the command line eg 如果您的MainProgram接受一些参数,它们也会在命令行中传递它们,例如

       java -cp myprogram.jar MainProgram argument1 argument2

Some of the SDKs (like Eclipse) have a function for the creating of jar-files. 一些SDK(如Eclipse)具有创建jar文件的功能。 If you work one the console this will maybe help you to create stable jar-files: 如果你在控制台上工作,这可能会帮助你创建稳定的jar文件:

'jar cfm className.jar MANIFEST.MF className.class' 'jar cfm className.jar MANIFEST.MF className.class'

  1. This should not matter, except for point 4. 除第4点外,这无关紧要。
  2. You would have to tell them the minimum required version. 您必须告诉他们所需的最低版本。
  3. You could, but then you would probably have to create an installer, which is even more complicated than a jar. 你可以,但是你可能需要创建一个安装程序,它比jar更复杂。
  4. In general yes, if your user has a VM at least the version you compiled with. 通常是的,如果您的用户至少拥有您编译的版本的VM。 The class files that you package inside the jar are targeted to some version of the JRE and their format changes every now and then. 您在jar中打包的类文件将针对某个版本的JRE,并且它们的格式会不时发生变化。 Higher versions of the JRE support lower version class files. 更高版本的JRE支持较低版本的类文件。

The error message indicates that the JRE can't find your main class. 错误消息表明JRE找不到您的主类。 From the comments it looks like you did not specify the manifest correctly. 从评论看起来您没有正确指定清单。 Is it contained inside a META-INF directory inside the jar? 它是否包含在jar内的META-INF目录中? What does jar -tf my_jar.jar tell you? jar -tf my_jar.jar告诉你什么?

If you're worried about JRE versions, try specifying a lower target version in your javac command. 如果您担心JRE版本,请尝试在javac命令中指定较低的目标版本。 You do this by adding -target xx to your javac invocation and possibly also -source xx . 你可以通过在你的javac调用中添加-target xx来实现这一点,也可以在-source xx Have a look at javac -help for a description of the flags. 请查看javac -help以获取标志的描述。

You can use jlink . 你可以使用jlink It comes with the JDK. 它配备了JDK。 It helps distribute applications without having to install Java. 它有助于分发应用程序而无需安装Java。

Using Java 9 Modularization to Ship Zero-Dependency Native Apps mentions that - 使用Java 9模块化发送零依赖性本机应用程序提到 -

jlink bundles a stripped-down JRE with only those modules that your application needs. jlink将精简版JRE与仅应用程序所需的模块捆绑在一起。

An oracle blog article titled Faster and Easier Use and Redistribution of Java SE mentioned that - 一篇题为“ 更快,更容易使用和重新分发Java SE ”的oracle博客文章提到 -

Using the 'jlink' tool introduced with JDK 9 will make it even easier for application developers to package and deploy dedicated runtimes rather than relying on a pre-installed system JRE. 使用JDK 9引入的“jlink”工具将使应用程序开发人员更容易打包和部署专用运行时,而不是依赖于预安装的系统JRE。

Now to answer two of your your Questions - 现在回答你的两个问题 -

IF you use jlink, user would not have to know on which version he/she should run the application. 如果您使用jlink,用户无需知道他/她应该在哪个版本上运行该应用程序。 Moreover, You can package the correct JRE version with your App. 此外,您可以使用您的应用程序打包正确的JRE版本。 To make it more clear, Java Runtime Environment (JRE) is gone since JDK 11. jlink will build a dedicated JRE for you. 为了更清楚, Java Runtime Environment(JRE)从JDK 11开始就消失了.Jlink将为您构建一个专用的JRE。

JDK 11 Release Notes states - JDK 11发行说明指出 -

In this release, the JRE or Server JRE is no longer offered. 在此版本中,不再提供JRE或Server JRE。 Only the JDK is offered. 仅提供JDK。 Users can use jlink to create smaller custom runtimes. 用户可以使用jlink创建较小的自定义运行时。

Reddit has a nice comment regarding this issue. Reddit对此问题有一个很好的评论

There is no separate JRE anymore, only the JDK which includes all JRE parts (eg the java binary). 没有单独的JRE,只有包含所有JRE部分的JDK(例如java二进制文件)。 Also, do not use OracleJDK unless you intend to pay them money. 此外,除非您打算向他们付钱,否则不要使用OracleJDK。 OpenJDK is the new default. OpenJDK是新的默认值。

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

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