Deploying Java applications as JAR files SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick
Consider the following project SE-2030 Dr. Rob Hasker 2 This application consists of two source files in the same directory, in a package called modalDialog
When you run an application from within Eclipse, it issues the following command : java –cp “D:\My Documents\MSOE\Courses\Example Programs\se2030\Multithreading” modalDialog/DialogApp java is the command that runs the Java Virtual Machine Same as C:\Program Files\Java\jdk1.7.0_40\bin\java.exe DialogApp is the name of the main class modalDialog is the name of the package containing the main class -cp specifies the directory where the.class file(s) are located Not including the package SE-2030 Dr. Rob Hasker 3
The formal syntax is: java –cp / javaw is another command that runs the Java Virtual Machine without a console window is the name of the main class The one containing the main() method is the name of the package containing the main class Note the “/” separator after the package specification -cp specifies the classpath the directory containing the classes that make up the application If the classes are distributed among more than one directory, then is a semicolon-separated list of directory paths SE-2030 Dr. Rob Hasker 4
You can create a shortcut to run the application from the Desktop SE-2030 Dr. Rob Hasker 5 In Windows 7: 1. Right-click the Desktop and select New/Shortcut from the context menu that appears. 2. Browse to the place on your file system containing the Java VM, as shown (your location may be different).
Creating a shortcut, continued... SE-2030 Dr. Rob Hasker 6 3. Type the name for your shortcut that will appear beneath the shortcut icon on your desktop.
Creating a shortcut, continued... SE-2030 Dr. Rob Hasker 7 4. Select Properties of the resulting shortcut icon on your desktop. The dialog to the left appears. 5. Append the file path to the class file to the existing Target. 6. If it consists of only a single directory, the classpath can be specified in the “Start in:” text box, and the “-cp” option is not needed in the “Target” specification
Next, consider the following project SE-2030 Dr. Rob Hasker 8 This application consists of two source files in two different classpaths (src and auxiliary), but the same package (edu.msoe.se2030.demo)
Using the preceding project as an example : java –cp D:\My Documents\MSOE\Courses\Example Programs\se2030\JARDemo\src; D:\My Documents\MSOE\Courses\Example Programs\se2030\JARDemo\auxiliary edu.msoe.se2030.demo/JARDemoApp JARDemoApp is the name of the main class edu.msoe.se2030.demo is the name of the package containing the main class The classpath specifies both directories where the.class file(s) are located Separated by a semicolon SE-2030 Dr. Rob Hasker 9
Finally, consider the following project SE-2030 Dr. Rob Hasker 10 This application consists of one source file, but the WinplotterDemo project uses an external library (winPlotter.jar) containing several user-written classes
Using this last project as an example : java –cp “D:\My Documents\MSOE\Courses\Example Programs\se2030\WinplotterDemo; D:\My Documents\MSOE\Courses\Example Programs\jars\winPlotter.jar” edu.msoe.se2030.plot/WinplotterDemoApp WinplotterDemoApp is the name of the main class edu.msoe.se2030.plot is the name of the package containing the main class The classpath specifies both the directory where the WinplotterDemoApp.class file is located As well as the path to the winPlotter.jar file that contains the external user-written classes SE-2030 Dr. Rob Hasker 11
A Java Archive (JAR) file enables you to bundle multiple files into a single archive file A JAR file is essentially a ZIP file with specific contents: The files you want to zip into the file.class files Source files (.java) if you want to enable debugging Javadoc files if you want to provide context-sensitive help for the classes in the JAR file A manifest file (MANIFEST.MF) Which specifies what’s in the JAR file SE-2030 Dr. Rob Hasker 12
The jar utility is used to create JAR files jar cfm jar is the command that runs the jar utility Same as C:\Program Files\Java\jdk1.6.0_03\bin\jar.exe jarfile is the name of the JAR file you want to create manifest is the name of a file containing manifest information Note : The contents of the manifest must be encoded in ansi. files specifies the files you want to place in the JAR file Separated by spaces SE-2030 Dr. Rob Hasker 13
To bundle files into a JAR file: jar cfm MyDialog.jar manifest.txt modalDialog/DialogApp.class modalDialog/MyDialog.class (assuming you are issuing the jar command from within the project directory of D:\My Documents\MSOE\Courses\Example Programs\se2030\Multithreading) manifest.txt is a text file (created with Notepad) containing the following text: Manifest-Version: 1.0 Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.) Main-Class: modalDialog.DialogApp SE-2030 Dr. Rob Hasker 14
To deploy your application, you just have to copy the JAR file to someplace on the target PC To run the application bundled within a JAR file, issue the following command: java –jar “D:\My Documents\MSOE\Courses\Example Programs\se2030\Multithreading MyDialog.jar” Or create a shortcut containing the above command SE-2030 Dr. Rob Hasker 15
Online tutorial “Packaging Programs in JAR files” ment/jar/index.html SE-2030 Dr. Rob Hasker 16