Instructions for Running a JAR File

Updated January 18, 2015

For Windows Users

To get started, save the JAR file to your desktop. For the purposes of these instructions, we'll pretend your JAR file is named myfile.jar.

Opening the Command Prompt:

From the Start button (in the lower left), type 'cmd' (without quotes) in Search Programs and Files, and click on the first item (it will be either cmd or cmd.exe). This should open up a window that has a path to a directory followed by a >. It probably looks something like: C:\Users\your_name_here>

This program is called a command prompt. You can use this to type various commands to the computer that can open, move, and edit files, as well as run programs (among manyother things).

You are currently in your home directory. Type 'dir' (which is short for 'directories') and press Enter. What do you see?

Navigating to Your Desktop

Your should have noticed 'Desktop' among other directories and files when you typed the 'dir' command. Now it's time to navigate to your Desktop.

Type 'cd Desktop' and press Enter. The 'cd' stands for 'change directory'. In this case, we want to change the current directory to the Desktop.

The bottom line should look like: C:\Users\your_name_here\Desktop>. By typing 'dir', you should be able to see all the files and folders located on your Desktop. One of these files is our JAR file, myfile.jar.

Running the JAR File

Now that we're on the Desktop, we can actually run our JAR file. To run a JAR file, type 'java -jar myfile.jar'. This tells the computer that you want to use Java to run a JAR file called myfile.jar.

Chances are the Command Prompt printed out some lines of text that aren't what you expected. For example, if you do not have Java installed on your computer, an error saying "java: command not found" may have appeared. Most likely, you have Java installed, but there is another, different problem with running the JAR file: the JAR file may require certain inputs (or arguments) in order to do its job correctly. In this case, your command prompt is displaying a usage description (e.g. Usage: xml2csv-conv [-options] <source filename or url> <destination filename>) and also a list of options usable with this JAR file.

For example, the XML-to-CSV converter (xml2csv-conv.jar) on the CSCI 0931 resources page requires two arguments: a source filename and a destination filename. To supply the JAR with these arguments, simply type them after the 'java -jar xml2csv-conv.jar', each separated by space: java -jar xml2csv-conv.jar my_original_data.xml my_converted_data.csv.

Mixing in Options

When you run certain commands from the Command Prompt, you can include options to modify the command. These options are typically specified below the usage description. You can find these by running a JAR file that requires arguments without any arguments or just by Googling the command for documentation.

To use an option, type it's flag (usually a dash followed by a letter or two dashes followed by a word) between the command's name and any arguments you provide. The -jar in java -jar myfile.jar is an option for the Java command that tells Java to expect a JAR file (rather than a '.java' file).

In the xml2csv-conv.jar, one possible option is '-s <value>'. CSV separates by commas by default (hence the name, Comma Separated Values), but you can use the '-s' option to specify another separator in place of the commas. The <value> is a special argument required by the option -- it indicates what replacement separator to use. For example, this command would run the XML-to-CSV converter and create a CSV file with elements separated by dashes instead of commas: java -jar xml2csv-conv.jar -s "-" my_original_data.xml my_converted_data.csv

Commands with multiple options and arguments can get confusing, so don't be afraid to come to office hours if you need a deeper explanation!

On Mac

To get started, save the JAR file to your desktop. For the purposes of these instructions, we'll pretend your JAR file is named myfile.jar.

Opening the Terminal:

Open Spotlight by clicking the magnifying glass in the upper righthand corner or by pressing Command and Spacebar simultaneously. Type 'Terminal' into the search bar and click the first result (with an icon that looks like a black box with white symbols in the upper left corner). This should open up a window that has a path to a directory followed by a >. It probably looks something like: ComputerName:~ your_name$

You can use the Terminal to type various commands to the computer that can open, move, and edit files, as well as run programs (among manyother things).

You are currently in your home directory (that's what the ~tilda~ symbol indicates). Type 'ls' (which is short for 'list') and press Enter. What do you see?

Navigating to Your Desktop

Your should have noticed 'Desktop' among other directories and files when you typed the 'ls' command. Now it's time to navigate to your Desktop.

Type 'cd Desktop' and press Enter. The 'cd' stands for 'change directory'. In this case, we want to change the current directory to the Desktop.

The bottom line should look like: ComputerName:Desktop your_name$ . By typing 'ls' again, you should be able to see all the files and folders located on your Desktop. One of these files is our JAR file, myfile.jar.

Running the JAR File

Now that we're on the Desktop, we can actually run our JAR file. To run a JAR file, type 'java -jar myfile.jar'. This tells the computer that you want to use Java to run a JAR file called myfile.jar.

Chances are the Terminal printed out some lines of text that aren't what you expected. For example, if you do not have Java installed on your computer, an error saying "-bash: java: command not found" may have appeared. Most likely, you have Java installed, but there is another, different problem with running the JAR file: the JAR file may require certain inputs (or arguments) in order to do its job correctly. In this case, your terminal is displaying a usage description (e.g. Usage: xml2csv-conv [-options] <source filename or url> <destination filename>) and also a list of options usable with this JAR file.

For example, the XML-to-CSV converter (xml2csv-conv.jar) on the CSCI 0931 resources page requires two arguments: a source filename and a destination filename. To supply the JAR with these arguments, simply type them after the 'java -jar xml2csv-conv.jar', each separated by space: java -jar xml2csv-conv.jar my_original_data.xml my_converted_data.csv.

Mixing in Options

When you run certain commands from the Terminal, you can include options to modify the command. These options are typically specified below the usage description. You can find these by running a JAR file that requires arguments without any arguments or just by Googling the command for documentation.

To use an option, type it's flag (usually a dash followed by a letter or two dashes followed by a word) between the command's name and any arguments you provide. The -jar in java -jar myfile.jar is an option for the Java command that tells Java to expect a JAR file (rather than a '.java' file).

In the xml2csv-conv.jar, one possible option is '-s <value>'. CSV separates by commas by default (hence the name, Comma Separated Values), but you can use the '-s' option to specify another separator in place of the commas. The <value> is a special argument required by the option -- it indicates what replacement separator to use. For example, this command would run the XML-to-CSV converter and create a CSV file with elements separated by dashes instead of commas: java -jar xml2csv-conv.jar -s "-" my_original_data.xml my_converted_data.csv

Commands with multiple options and arguments can get confusing, so don't be afraid to come to office hours if you need a deeper explanation!