Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
student:java:jar [2018/08/10 13:17] bernstdhstudent:java:jar [2023/09/04 16:19] bernstdh
Line 1: Line 1:
- 
- 
 ===== The Java Archive Tool (jar) ===== ===== The Java Archive Tool (jar) =====
  
Line 8: Line 6:
  
  
-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.+
  
  
Line 53: Line 48:
 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). 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).
  
 +==== 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%%'', 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>+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>.
  
 ==== Testing an Executable .jar File ==== ==== Testing an Executable .jar File ====
  
- +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 (sometimes also called a terminal), change the working  directory to the directory that contains the ''%%.jar%%'' file (using the ''%%cd%%'' command),  and then execute the application as follows:
-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: +
- +
  
 |   java -jar //filename//.jar   | |   java -jar //filename//.jar   |
  
 +If you get a message like the following:
  
- +<code> 
-==== Using Resources in .jar File ==== +Exception in thread "main" java.lang.UnsupportedClassVersionError 
- +The program has been compiled by 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
-You may want to include files other than ''%%.class%%'' files  in ''%%.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). +
- +
- +
-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> </code>
  
-You could then load it as follows:+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).
  
-<code java> +==== Using Resources in a .jar File ====
-ImageIcon icon new ImageIcon(url); +
-</code> +
- +
-You can get an ''%%InputStream%%'' for a resource in a similar fashionFor 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. 
  
 +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.
  
 ==== Changing the Icon of Executable .jar Files ==== ==== Changing the Icon of Executable .jar Files ====