What can be done if the java jar file could not be launched?

557    Asked by JenniferFraser in Java , Asked on Oct 6, 2022

 I'm trying to download the file http://algs4.cs.princeton.edu/code/algs4.jar and launch it with Jar Launcher (the default application). 

Following https://discussions.apple.com/thread/4827576?tstart=0, I've also tried to use Archive Utility; this appears to create an algs4.jar.cpgz file but I'm not sure what to do with it. How can I open the JAR file?

Answered by Jessica Gibson

Even though the error message says to check the Console because the java jar file could not be launched, I couldn't find anything useful there. However if you try to run the JAR file from the Terminal you get this error:


no main manifest attribute, in ./algs4.jar

This error means that the JAR file is missing the "Main-Class" parameter in its MANIFEST.MF file. The Main-Class parameter tells the Java runtime which class is the Entry Point, i.e. which class to run first when launching.

You can add this parameter using the Terminal. First of all download the JAR file from the Princeton website, then run the following command (assuming the JAR file is in your Downloads folder):

jar ufe ~/Downloads/algs4.jar TestAlgs4

This command updates the MANIFEST.MF file within the JAR. Once that is done the JAR file should run from the Finder or the Terminal successfully.

Having said all that, you only get the error if you try to run the JAR like an executable. I don't think you're meant to do that. You are meant to just place the JAR file somewhere the Java runtime can find it, and then refer to it in the code you write as you do the exercises in this book. There are clear instructions here on what you are supposed to do with the JAR file here: https://algs4.cs.princeton.edu/code/



Your Answer

Interviews

Parent Categories