Author avatar

Maciej Duraj

Creating and Opening JAR Files Using the Java Programming Language

Maciej Duraj

  • Apr 30, 2019
  • 16 Min read
  • 2,127 Views
  • Apr 30, 2019
  • 16 Min read
  • 2,127 Views
Java Frameworks
Java

Introduction

Java is a great language to get into and get an intro to programming with. Like HTML and Javascript (a separate language that runs on browsers and not virtual machines), it is a language that is written in plain text. Thus, it can be edited and written using Textedit or Notepad, however, you will also be using the command prompt. Java is an object-oriented and general-purpose programming language, utilizing the JAVA Runtime Environment that lets application developers take a ”write once, runs anywhere” (WORA) approach to coding.

This simplifies things with many operating systems, endpoint devices, and even browsers available for programs to run off of. This is where the JAR file format comes in. In a nutshell, these files contain JAVA applications in a package environment.

Keeping files secure, archived, and reliable across different operating systems and browsers is key for JAVA programmers. This is due to the programming language’s nature, which I will get into below.

Jar Files Explained

Jar extension (.jar) is an archiving format designed to structure and compress JAVA data files in one file class built on the ZIP format. It also has some advantages over other file formats such as being able to contain metadata and program resources (text, images) within its data structure. Such files are able to run with as few implementation dependencies as possible; additionally, compiled Java code can run on all platforms that support it without the need for recompilation. This saves time and redundancy.

Before you begin, however, make sure you have all the Java programming language software installed to start coding and compiling code. You will need to have the latest Java Development Kit or JDK, which as of this writing is JDK 11 and you can find it here. It is also recommended that you download Eclipse IDE as a tool that will help you in any future Java coding (debugging, compilation and more). You can download the latest version here. Now let's get to creating a JAR file or what this tutorial is about.

Benefits and Reasons for Using JAR Files

Portable container of classes

A JAR file is like an archive file, which is able to contain multiple JAVA class file types along with metadata within its archive. The benefit of such a portable container is that the .jar file can be distributed as application software or as JAVA libraries. One file can easily be compressed and later shared or uploaded online. It can also easily be unarchived. Other benefits of storing or running JAR files include lossless data compression, archiving, decompression, and archive unpacking.

Creating Jar Files (Archived Java Class Files)

Here is a way to create a Jar file. First of all, you will need to be working with Java class files. A Java class file (.class) is a file that contains Java byte code, compiled from a Java source files (.java extension) and can be executed on a Java Virtual Machine (VM). These file types have a .class extension. The metadata in the JAR file has to be encoded in UTF8.

Thus, if you do not have a class file created, you will need to do that first. In order to do that, open a text editor and type the following statement (for our purposes, the name of the class file will be “JarClassFile.”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class JarClassFile
        Public static void main  (String[]  args)  {
            System.out.println(“We will put this class in a jar file”);
        }
}

More information on using this particular class file type as it relates to JAR files can be found [here]( https://www.webucator.com/how-to/how-create-jar-file-java.cfm)

Now, exit the text editing program of your choosing (such as Notepad on Windows) and navigate into the command prompt by clicking the Windows icon on your keyboard alongside the key “R.” Then input “cmd” inside the prompt to run the command prompt. Then, navigate to the directory where your java class files are kept by inputting cd or change directory and inputting the directory where your java files are kept. For our purposes, lets name it “JavaStorage.”


```[Java (JDK 8-11) using Windows command prompt]

CD JavaStorage
[Java

Then click enter.

Afterward, input this into the command prompt you have open to see what java files are present.

1
dir
[Java

The files with the .java extension are your Java files you can compile into a JAR file. Lets say we want our final JAR output file to be called simply “Output.” Here is how we create it from within the command prompt:

1
Jar –cf Output.jar *.java
[Java

Then, hit the enter key and it should compile. You can use your previously created class file here for demonstration purposes or what we named JarClassFile.class at the end of the above code if it was compiled (along with the v input producing a verbose output that tells you the name of each file as it is added to the JAR archive) or the .java save you created. For example:

1
Jar –cvf Output.jar JarClassFile.class
[Java

If you want specific file archived, where it says *.java, input the specific .java file names you want archived. For instance, lets say we have “file 1” and “file 2.” This is how the cod would then look like:

1
jar –cf Output.jar file1.java file2.java
[Java

You can either click dir again to see that the JAR file exists and was created or go to the directory by navigating via your Windows (or another operating system you are using) to the directory you store your files and in which you originally pressed cd onto within the command prompt. In our case, it was “JaveStorage.”

Jar cf means to create a jar file; jaf-file is the name of the file you choose to create, and the input file(s) is a space-separated list of files you want to include or contain within the jar file or container itself. By the way, for our purposes, let’s use “bunny” as our jar-file name.

You can also add various jar command options to the code showcased above. For instance, 0 signifies that you do not want the jar file compressed; v produces a verbose output that tells you the name of each file as it’s added to the JAR file; m means that the default manifest file should not be produced; and -c is creating to changing directories for the command file execution. The code below is an example of creating a jar file and using a couple of the commands from above:

1
jar cvf0 Bunny.jar JarClassFile.class audio images
[

This code says that an uncompressed jar file with a verbose output will be created called Bunny (within .jar file name). The final jar file will also include various audio files from the JarClassFile class file structures that will be archived within the .jar executable.

Running Jar files Using JAVA on Windows 10

Windows 10 is Microsoft’s latest operating system and one most businesses rely on today. Thus, you will most likely be developing and running JAVA applications on this OS.

Before we begin, make sure you have Administrator privileges. This will grant you the privilege to work with all sorts of file formats, not necessarily restricted to JAR, and edit various files.

Now let’s take a look at how to run JAR files on Windows 10. First of all, there are two main methods. Below, I will explain the proper and more personal method of running JAR files on Windows, which is the .bat file format method. If you want a simpler method that is more automatic and based on additional software that may do the job for you with fewer steps, you may be able to get it to work by using a utility called JarFix.exe. It supposedly fixes the associations automatically and allows Windows 10 to execute them with a simple double click -- as long as you have JRE or JDK installed. Download JarFix here. Now, if the utility is hard for you to use or you do not want to bother with it for some reason or it doesn’t fix the problem, read on for the standard .bat method.

  1. Download the necessary files.
  2. Unzip and locate your .jar file.
  3. Check to see if the file is clickable or has an extension showing.
  4. Make the file executable through a second .txt and later .bat file present.

Download the Necessary Files

First of all, make sure you have the correct files installed to get started. You will need to have Java SE Development Kit for developers (JDK or Java SE), a ZIP file archiver such as 7-Zip installed (preferably), or WinZip and Winrar. If you do not need to compile code, but simply open or run the .jar files, then simply installing the Java Runtime Environment (JRE) will be enough (included in JDK). Another handy tool to have is the JRAR_Runner. You can read more information about the difference between downloading JDK and JRE to run or compile files here.

You can download Java Runtime Environment (JRE) here. The latest version, as of this writing, is Version 8 Update 151. While the more robust and developer-focused Java Software Development Kit (JDK) can be downloaded from the JRE [Oracle’s website. The current version, as of this writing, is Java SE 9.0.4. In terms of file archivers, 7-zip is recommended, and you can download it here.

Another useful program to have installed is JRAR_Runner, which you can download from here. This program can be used to simplify .jar associations as the application locates your Java installation and directs the Jar files to work within the Java Runtime framework. It may offer similar features to JarFix.

Unzip and Locate Your .jar File

Use one of the above archivers to open a .jar file you downloaded off the Web or from another server that is housed within a ZIP file. This unzips it into an archiver that you can see and select the Jar file within. If the zip file isn’t associated with any file archiver you have installed (or when installing them you didn’t specify to have any associations), you can right-click the zip file and click on ”Open With….” Then, select Winrar, 7zip or other zip file archiving software you have installed.

You will recognize a JAR file and its association with Java because it will have the Java coffee cup emblem on it when you hover your mouse over it. When you see the file sitting in the folder where you saved it or that it unzipped into, you may not be able to click on it right away. It may not even show the .jar extension, so we will need to make this visible first.

Check to See if the File Is Executable or Has an Extension Showing

First of all, take a look at the file, which should have the Java icon on it with the coffee cup. Also, take note whether it has the .jar extension showing or just the file name. If it does, you may be able to click on it and run it – or, if there is a .bat file next to it, you may run it through that file. If you cannot run it and do not see the .jar extension, do not panic.

In order to add a .jar association, and be able to execute the file, you will need to unhide the hide extension setting. In order to do this, you will have to go to View and then Options. A drop-down menu will appear with a couple of different things to select. Click on the first option that says, ”change folder and search options.”

A new window will appear with folder options as its title. Click on the View tab on the side of the General tab that you have currently opened. You will see many different settings, some marked and some not, in a typical Windows-like environment. Scroll all the way down until you see ”Hide extensions for known file types.” Uncheck it and then click apply on the lower-right section of the window. Then click Ok. This will finally allow you to see that the file you had was a .jar file. Beforehand, the file may not have shown an extension of any kind. It will also allow you to see the .txt for the text documents you may have used for editing Java software.

Make the File Executable Through a Second .txt and Later .bat File Present

Right-click with your mouse onto the open section of the folder your file is sitting in. Then, click on text document to create a blank document. Rename this .txt file ”RUN.” A pop-up will appear warning you that changing a file name might make it unusable. Ignore this warning and click yes. The .bat file you just renamed will show two cogwheels as its new icon.

Next, copy the entire name of your .jar file that is sitting next to the .bat file. Right-click the .bat file with the cogwheels as an icon and click on ”Edit.” Then type in: Java -Jar and right next to it (with a space first after Java like I have to show and one afterward) paste in the entire file name you previously copied. This includes the .jar extension. An example of what it may look like:

1
java -jar Shaders\<od-v2.4.9mc1.8.jar
[Java

Next, exit by clicking the x icon on the upper left of the window and click on ”Save.” You will then double click on the .bat file and it will open a new window with the extension you previously pasted showing among other information such as the user accessing the documents. It should also open your file you want to work on.

If you still run into errors or want to run more specific code or files, check out Oracle’s guide, which may be a bit harder to follow by novices, but also includes information on executing applets packaged in .jar as well as code that you may want to use as an extension. One of the things to consider if you are still running into problems running JAR files is to make sure that your runtime environment has information about which class within the file is the application’s entry point. To indicate this information, add a Main-Class header to the JAR file’s manifest in a form of the header:

1
Main-Class: Classname
[Java

Conclusion

Java is a programming language that has been around for more than 20 years. It is a good language to learn programming with because many apps (particularly Android and Web apps) rely on it and it can be implemented across various browsers and operating systems. It is also designed to have the feel of C++ but is much simpler to use and get into with its object-oriented architecture. To learn more about programming in JAVA you can check out this in-depth tutorial.

As you see, Java is a great language to get into and learn as well as providing a great framework for other programming languages. In addition, it is text-based, thus requires no additional programming software (beyond what your Mac, Linux, or Windows PCs you own should come with). Java can also easily be compiled from terminal commands (more info here). Now is the perfect time to step into the world of Java programming.

3