Thursday, May 13, 2010

Failed to Load Main-Class Manifest Attribute......

A common problem encountered by almost every java beginners, including me myself, is having this error,

Failed to load Main-Class manifest attribute …”,

when they are trying to execute a compiled executable .jar file through command prompt.

Everything needed to run a jar file has been set, e.g. classpath, manifest file, etc. Yet after multiple checking and rechecking, you still unable to get the jar file run and, as a result, you get even more frustrated.

Take a look at the error message. What it says is that java.exe could not find the main class of our java application. The main class itself, as the entry point of a java application, is specified in file MANIFEST.MF.

So, you have to make sure that the main class is actually specified in the manifest file, i.e. the following line is available somewhere in your manifest file:

Main-Class: MyMainClass

If you doubt that, then open your .jar file through an archiver (WinZip, WinRar, etc), go to folder META-INF, double-click MANIFEST.MF and see if that line is already there, if not, then add the above line to the file and save it.

Oh… in case you don’t notice… MyMainClass is actually your main class name, so replace it with the full name of your main class (the name of the class along with its package, if any)

There is one more fact you need to know about manifest file. You need to put a line break after the last line of the file. Fail to do so, the last line of the file won’t be parsed properly. So, in accordance with that, don’t forget to insert a line break to the line where the main class is specified, otherwise the error message will still pop up.

Wednesday, May 12, 2010

Executable .jar file is opened in an archiver (WinRar, WinZip, etc)


Something has gone wrong and when you try to open an executable .jar file by double-clicking it, the file is not executed by the JVM like it should be (before), instead it’s opened by an archiver software like WinRar or WinZip.

You can still execute the file though by manually select the java launcher through right-click – ‘Open with’ menu, but that is not the proper nor the right way to do, and nobody wants to do it that way.

This problem happens because the .jar file association has been changed to open the archiver software. The .jar file itself is an archived file (jar stands for java archive), so, not surprisingly, most available archivers out there can open it, meaning they will display the full content of it (class file, java file, manifest file, etc.) instead of running it.

Now, we need to reset the file association for the .jar file so that the JVM will be one who opens and therefore launches the .jar file, not some archivers. In order to do that in Windows XP, follow these steps:
  1. Open Windows Explorer.
  2. Open Tools – Folder Options
  3. Open tab File Types – select ‘.Jar’ in Registered File Type List – click button ‘Advanced’
  4. select ‘Open’ in the Action list – click ‘Edit’
  5. Type into textfield ‘Application used to perform action’ the following line
    ”[JAVA_HOME]\bin\javaw.exe” –jar “%1%” %*

note: replace [JAVA_HOME] with your jre path (e.g. C:\Program Files\Java\jre1.6.0_03) and don’t forget to type the quotes.

For Windows Vista/7 users, you can’t do those steps through Folder Options because the tab File Types is not available on vista/7 no more. Yet, no need to worry, there is a free software from sourceforge.net called ‘Types’ which provides an alternative way to set actions for file types. This is a very lightweight application, its current version is Types 1.3.1 and only takes 155 KiB of your harddrive. You can download it at http://types.sourceforge.net/. Below is a snapshot I took from the the download link.




There you go. You now have reset .jar file to associate with its launcher, javaw.exe, and can now run your jar file on your PC again by directly double-clicking it.