anichhangani
anichhangani

Reputation: 129

Distributing java application

I have recently developed some java applications that i want others to run on their machines. I did some research and now know that to distribute java code you need to create .jar file. 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.
  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. how??
  4. Are jar files not compatible with other version of jre except in which they are compiled.

Upvotes: 5

Views: 469

Answers (3)

jackrabbit
jackrabbit

Reputation: 5653

  1. This should not matter, except for point 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.
  4. In general yes, if your user has a VM at least the version you compiled with. 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. Higher versions of the JRE support lower version class files.

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

If you're worried about JRE versions, try specifying a lower target version in your javac command. You do this by adding -target xx to your javac invocation and possibly also -source xx. Have a look at javac -help for a description of the flags.

Upvotes: 0

Daniel05
Daniel05

Reputation: 105

Some of the SDKs (like Eclipse) have a function for the creating of jar-files. If you work one the console this will maybe help you to create stable jar-files:

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

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34367

Option1: Create a manifest file with entry as below and include in the jar:

     Main-Class: MainProgram

Option2: Just mention your Main class name while running the program e.g. if you jar name is myprogram.jar and main class is MainProgram then run the program as below:

        java -cp myprogram.jar MainProgram

If your MainProgram takes some arguments, them pass them as well in the command line e.g.

       java -cp myprogram.jar MainProgram argument1 argument2

Upvotes: 2

Related Questions