Next revision | Previous revisionNext revisionBoth sides next revision |
student:java:jar [2018/08/10 13:16] – created bernstdh | student:java:jar [2024/01/23 10:55] – bernstdh |
---|
| |
| |
===== The Java Archive Tool (jar) ===== | ===== The Java Archive Tool (jar) ===== |
| |
| |
| |
The ''%%jar%%'' tool can be used to bundle multiple files into a single file. JAR files typiclaly contain the class files and resources (e.g., images) required by an application. | The ''%%jar%%'' tool can be used to bundle multiple files into a single file. JAR files typically contain the class files and resources (e.g., images) required by an application. |
| |
| |
Resources in a JAR file can be accessed from the application using a java.net.URL object created using the java.lang.Class#getResource(java.lang.String) method or the java.lang.Class#getResourceAsStream(java.lang.String) method or the in the ''%%Class%%'' class. | |
| |
| |
| |
| |
| jar cf //jar-file////input-files// | | | jar cf //jar-file// //input-files// | |
| |
| |
You can create an executable JAR file by including a manifest that includes a ''%%Main-Class:%%'' directive. For more information, see the [[ http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html | tutorial ]]. | You can create an executable JAR file by including a manifest that includes a ''%%Main-Class:%%'' directive. For more information, see the [[ http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html | tutorial ]]. |
| |
| ==== Running an Executable .jar File ==== |
| |
==== Permissions and Executable .jar Files ==== | You can run an executable .jar file from a command shell/terminal window as follows: |
| |
| - Open a command shell/terminal window. |
| - Change the working directory to the directory that contains the ''%%.jar%%'' file (using the ''%%cd%%'' command) |
| - Execute the application as follows: ''%% java -jar //filename//.jar %%'' |
| |
Depending on your operating system, you may need to change the permissions of the executable ''%%.jar%%'' file (i.e., provide permission to execute). In most OSs this can be done from the command line (using ''%%chmod%%'') or from the GUI (in a variety of different ways). | You can also run an executable .jar file from a file explorer/finder by double-clicking on it. However, your associations and permissions must first be setup. |
| |
| ===== Permissions and Executable .jar Files ===== |
| |
You may also need to set the default manner in which it is "opened" (i.e., instruct the OS to used the Java runtime). This is sometimes also called the files "association" or "default program". From the command line: in MS Windows you can use ''%%assoc%%'', is OS X you can edit ''%%~/Library/Preferences/com.apple.launchservices.plist%%'', and in Linux you can edit ''%%/usr/share/applications/defaults.list%%'' From the GUI: in MS Windows you can click on <key>Start</key>+<key>Default Programs</key>, in OS X you can right-click on the file and use <key>File</key>+<key>Get Info</key>, and in Linux you can right-click and select <key>Properties</key> | Depending on your operating system, you may need to change the permissions of the executable ''%%.jar%%'' file (i.e., provide permission to execute). In most OSs this can be done from the command line (using ''%%chmod%%'') or from the GUI (in a variety of different ways). |
| |
==== Testing an Executable .jar File ==== | ===== Associations and Executable .jar Files ===== |
| |
| You may also need to set the default manner in which it is "opened" (i.e., instruct the OS to used the Java runtime). This is sometimes also called the files "association" or "default program". From the command line: in MS Windows you can use ''%%assoc%%'', in OS X you can edit ''%%~/Library/Preferences/com.apple.LaunchServices.plist%%'', and in Linux you can edit ''%%/usr/share/applications/defaults.list%%'' From the GUI: in MS Windows you can click on <key>Start</key>+<key>Default Programs</key>, in OS X you can right-click on the file and use <key>File</key>+<key>Get Info</key>, and in Linux you can right-click and select <key>Properties</key>. |
| |
If you just click/double-click on an executable ''%%.jar%%'' file and the code throws an exception you (typically) won't see it. Hence, when testing it is often useful to run the application from the command line. To do so, open a command shell, change the working directory to the directory that contains the ''%%.jar%%'' file, and then execute the application as follows: | ==== Testing an Executable .jar File that You Created==== |
| |
| If you just click/double-click on an executable ''%%.jar%%'' file and the code throws an exception you (typically) won't see it. Hence, when testing it is often useful to run the application from the command line. To do so, see the relevant instructions above. |
| |
| If you get a message like the following: |
| |
| java -jar //filename//.jar | | <code> |
| Exception in thread "main" java.lang.UnsupportedClassVersionError |
| The program has been compiled by a more recent version of the Java Runtime (class file version XX.0), |
| this version of the Java Runtime only recognizes class file versions up to YY.0 |
| </code> |
| |
| it means that you need to update your version of Java. When doing so, you need to be careful because many systems have both the Java Runtime Environment (JRE) and the Java Development Kit (JDK) installed, and updating the one may not update the other. In Linux/OSX you can use ''%%which java%%'' and in MS-WIndows you can use ''%%where java%%'' to find the path to the Java interpreter. You can change the ''%%path%%'' environment variable (not the ''%%classpath%%'' environment variable) to change which Java interpreter is used (i.e., either the one installed with the JRE or the one installed with the JDK). |
| |
==== Using Resources in a .jar File ==== | ==== Using Resources in a .jar File ==== |
| |
| |
You may want to include files other than ''%%.class%%'' files in a ''%%.jar%%'' file (e.g., images, configuration files, input files). Such files are often called resources. To refer to a resource in a ''%%.jar%%'' file you should use a ''%%URL%%'' object (from the ''%%java.net%%'' package). | You may want to include files other than ''%%.class%%'' files in a ''%%.jar%%'' file (e.g., images, configuration files, input files). Such files are often called resources. See the discussion of |
| [[ student:java:resources | using resources ]] for more information. |
| |
The easiest way to get the appropriate ''%%URL%%'' object is to use a ''%%Class%%'' object. For example, if you want to get the ''%%URL%%'' for a resource named ''%%logo.png%%'' that is in the ''%%resources%%'' directory **under the directory containing the class of the object referred to by ''%%this%%''** then you would do the following: | |
| |
<code java> | |
URL url = this.getClass().getResource("resources/logo.png"). | |
</code> | |
| |
You could then load it as follows: | |
| |
<code java> | |
ImageIcon icon = new ImageIcon(url); | |
</code> | |
| |
You can get an ''%%InputStream%%'' for a resource in a similar fashion. For example: | |
| |
<code java> | |
InputStream is = this.getClass().getResource("resources/config.txt"). | |
BufferedReader in = new BufferedReader(new InputStreamReader(is)); | |
String line = in.readLine(); | |
</code> | |
| |
This technique uses the ''%%ClassLoader%%'' infrastructure to retrieve resources from the ''%%.jar%%'' file. So, it is very important that you understand the relative locations of the resource and the object that you call ''%%getClass()%%'' on. A leading ''%%/%%'' will start at the root of the directory tree, but you still must understand where in the ''%%.jar%%'' file is the root. | |
| |
==== Changing the Icon of Executable .jar Files ==== | ==== Changing the Icon of Executable .jar Files ==== |