5

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.
7
  • How did you create the JAR (what options, and did you include a manifest with Main-Class) and how are you trying to run the program? Commented Nov 8, 2012 at 20:54
  • I included the manifest.mf file with the jar using "jar xyz.jar manifest.mf *.class" command in cmd . I need that it should run on any machine by just double clicking on it.It runs on some machines which have jre 7, but otherwise on lower versions it gives above mentioned error. Commented Nov 8, 2012 at 20:58
  • What JDK version are you compiling it with? Commented Nov 8, 2012 at 21:00
  • 2
    +1; good question. You're facing a general problem when it comes to software distribution, especially in Java. Sadly, it's not that easy to solve. During development and build you may have to make (and communicate) certain decisions (min/max JRE version, JRE vendor, OS'es supported, OS behavior like double-click a JAR, etc.). All those factors influence your build process. I'd say you just have to work it out - apologies... Commented Nov 8, 2012 at 21:05
  • unless you have some code which is specific to JDK7 it would be better to compile with the lowest version of JDK possible. Then anyone who has a JRE at a higher version can use it. Commented Nov 8, 2012 at 21:05

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

13 Comments

Well i did that an it happens to run on some machines but on some other machines whose jre version is not the same as the app was compiled in it does not work
@AnirudhChhangani: When its not running, are you sure of getting Main class not found error?
Yeah absolutely...the case is when I upgrade the users JRE version to the version the app was compiled in it runs...
@AnirudhChhangani I'm willing to bet that the machines where it doesn't work have an older version of the JRE than the JDK for which you compiled it. The simplest solution is to set the --target flag for an older version of Java so that your program can run on them
If you are using ant, then refer javac task. It has an attribute as target. Refer javac cross compilation specification. This also has target option.
|
0

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'

Comments

0
  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.

4 Comments

the manifest is in the META-INF the above command gives me the list of all the classes inside the jar file. how can i compile a program in a lower version..can u specify the command
Just add -target my_version to your javac command. You need to be sure that you don't use any API from a newer version though.
Have a look at javac -help.
When I try to compile it with the command : "javac -target 1.6 xyz.java" an error returns saying target release 1.6 conflicts with default source release 1.7. how do i correct this??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.