Java Application Distribution

I recently developed some java applications that I want others to run on their machines. I did some research and now I know that to distribute java code you need to create a .jar file. Well, I did it, but when I distributed these files, it runs on some computers, but on others it returns an error: "The main class could not be found."

  • Is there a problem with the JRE version.
  • How the user finds out in which version he should launch the application.
  • Is it possible to pack the correct version of jre with my app / jar file. as??
  • Jar files are not compatible with another version of jre, except that they are compiled.
+5
source share
4 answers

Option1: create a manifest file with an entry as shown below and include in the jar:

Main-Class: MainProgram 

Option 2: just specify the class name Main at program startup, for example. if you are jar name myprogram.jar and the main class is MainProgram , then run the program as shown below:

  java -cp myprogram.jar MainProgram 

If your MainProgram takes some arguments, they pass them also on the command line, for example.

  java -cp myprogram.jar MainProgram argument1 argument2 
+2
source

Some of the SDKs (e.g. Eclipse) have a function for creating jar files. If you work on the console, this can help you create stable jar files:

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

0
source
  • It does not matter, except for paragraph 4.
  • You will need to tell them the minimum required version.
  • You could, but then you probably have to create an installer that is even more complicated than a jar.
  • In general, yes, if your user has a virtual machine, at least the version you compiled with. The class files that you pack in the jar are aimed at some version of the JRE and their format changes from time to time. Higher versions of the JRE support lower level class files.

The error message indicates that the JRE cannot find your main class. From the comments you can see that you did not specify the manifest correctly. Is it contained inside the META-INF directory inside the jar? What does jar -tf my_jar.jar ?

If you are worried about JRE versions, try specifying a lower target version in your javac command. You do this by adding -target xx to your javac call, and possibly also -source xx . See javac -help for a description of flags.

0
source

You can use Jlink . It comes with JDK. This helps distribute applications without having to install Java.

Using Java 9 Modularization to submit native zero-dependency applications mentions that -

jlink only integrates a stripped-down JRE with the modules your application needs.

An oracle blog article titled " Quick and Easy Use and Distribution of Java SE" mentions that:

Using the jlink tool introduced in JDK 9 will allow application developers to simplify the packaging and deployment of dedicated runtimes rather than relying on a pre-installed JRE system.

Now, to answer your two questions of yours -

If you use jlink, the user does not need to know in which version he should run the application. Alternatively, you can pack the correct version of JRE with your application. To make this clearer, the Java Runtime Environment (JRE) is missing from the JDK 11. jlink will create a dedicated JRE for you.

JDK 11 Release Notes:

In this release, JRE or Server JREs are no longer offered. Only JDK is offered. Users can use jlink to create smaller custom runtimes.

Reddit has a good comment on this.

There is no longer a separate JRE, only the JDK, which includes all parts of the JRE (for example, the Java binary). Also, do not use OracleJDK if you are not going to pay them money. OpenJDK is the new default value.

0
source

Source: https://habr.com/ru/post/1444936/


All Articles