Run a .Jar Java File
.jar files are used for archiving and archive unpacking. One of the essential features of .jar file is loss-less data compression. If you want to run a .jar java file, then you have to install it and make it executable before you can proceed. Because of their size, .jar files should prove to be an invaluable resource for sorting and sending files.
Contents
Steps
- Make sure you have Java installed on your system. Check this by typing java -version into the command terminal. If you don't have the latest version of Java, update it before proceeding.
- Make the .jar file executable. A .jar file is made executable by collecting the class files of your Java application; compilers or JVM (Java Virtual Machine) understand these formats. .jar files execute from javaw (Java web start). You need to set application's entry point within jar file. (An entry point is a class containing the main function of your application). You will set the entry point using the Manifest file. Here's how to do it:
- The Jar tool automatically sets meta-inf/manifest.mf as path of the .jar file. When you open a default Manifest file, it should read "Manifest-Version: 1.0, Created-By: 1.6.0 (Sun Microsystems Inc)"
- Make a manifest addition .txt file. Enter Main Class: [Package Name].[Class Name], with the bracketed portions filled in with your specific information. (This is in the form of an attribute value pair.) Do not forget to add an empty line at the end of the file.
- Enter this terminal command to modify the Manifest file to include the application's entry point, changing the bracketed portions to your specific file names: jar cfm [jar file name] [manifest-addition] [input files]
- Review your Manifest file. After you set the entry point, it should read: "Manifest-Version: 1.0,Created-By: 1.6.0 (Sun Microsystems Inc), Main Class: [Package Name].[Class Name]"
- Alternatively, you can set the entry point using the .jar tool. This overrides the Main-class attribute in the Manifest's file. Enter the following terminal command: jar cfe [jar file name] [Package Name] [Class Name with main function]
- Run your .jar file. Run the .jar file using the following command (the main method of your java application executes): java -jar [Jar file Name]
- Make it so that you can run your .jar file by double-clicking it (optional). To open the .jar file by double-clicking on it, change the directory to the location of javaw (java web start) executable. Type the following command into terminal: C:\Program Files\Java\j2rex.y.z\bin\javaw.exe" -jar "%1" %*
Running a .jar File in Linux
- Open terminal. With Linux, double-clicking the .jar file opens it in a zip archiver showing the contents of the .jar file. Instead, right-click and select "Open Terminal."
- Execute the following command: $>java -jar [jar file name]
- Open your .jar file by double-clicking (optional). If you want to run the .jar file by double-clicking, then you might have to try changing the file type association (similar to the instructions for Windows) for your flavour of Linux system.
- Make sure the above command works.
- Type the following command into terminal: $>/usr/lib/jvm/jre-1.6.0-sun/bin/java -jar %f
- Set the path according to where Java is located on your system.
Tips
- A .jar might depend on other files to work (just like .exe files do). If they are not in your classpath, your program might not work.
- In the folder with the javaw.exe, there is a program called javaws.exe. Don't mix them up - javaw.exe is for running java programs in windows, javaws.exe is JAVA web start.
- For help on creating a .jar file, see this article.
- Java programs should work on all platforms. If one does not, either it was not coded properly, or it is a very specialized program that calls on system resources or other programs.
- A .jar file may be a program or a library. if it's a library (it has no executable class inside), it's no use trying to run it.
Warnings
- Java is safer than most programming languages, but malware written in Java still exists. Let the user beware of what software is running in his machine.
Related Articles
- Create JAR File
- Program in Java
- Install Java
- Use Graphics in a Java Applet
- Install Java on Linux
- Enable JavaScript on an Android Phone
- Get Java on Android
- Change Java Runtime
- Edit the Contents of a .Jar File on a Mac
- Run a HTML File
- Install Netbeans on a Linux
Sources and Citations
- Sun Microsystems Java Homepage - Download the java runtime software for free
- Roedy Green’s Java & Internet Glossary Learn what those "Javanese" words mean