160 likes | 273 Views
Deploying Java applications as JAR files. Based on material by Dr. Mark L. Hornick. Consider the following project. This application consists of two source files in the same directory, in a package called modalDialog.
E N D
Deploying Java applications as JAR files Based on material by Dr. Mark L. Hornick SE-2030 Dr. Rob Hasker
Consider the following project This application consists of two source files in the same directory, in a package called modalDialog SE-2030 Dr. Rob Hasker
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 javais 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 <classpath> specifies the directory where the .class file(s) are located • Not including the package SE-2030 Dr. Rob Hasker
The formal syntax is:java –cp <path> <package> / <main_class> javawis another command that runs the Java Virtual Machine • without a console window <main_class> is the name of the main class • The one containing the main() method <package> is the name of the package containing the main class • Note the “/” separator after the package specification -cp <classpath> specifies the classpath • the directory containing the classes that make up the application • If the classes are distributed among more than one directory, then <dir> is a semicolon-separated list of directory paths SE-2030 Dr. Rob Hasker
You can create a shortcut to run the application from the Desktop 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). SE-2030 Dr. Rob Hasker
Creating a shortcut, continued... 3. Type the name for your shortcut that will appear beneath the shortcut icon on your desktop. SE-2030 Dr. Rob Hasker
Creating a shortcut, continued... • Select Properties of the resulting shortcut icon on your desktop. The dialog to the left appears. • Append the file path to the class file to the existing Target. • 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 SE-2030 Dr. Rob Hasker
Next, consider the following project This application consists of two source files in two differentclasspaths (src and auxiliary), but the same package (edu.msoe.se2030.demo) SE-2030 Dr. Rob Hasker
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
Finally, consider the following project This application consists of one source file, but the WinplotterDemo project uses an external library (winPlotter.jar) containing several user-written classes SE-2030 Dr. Rob Hasker
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.plotis 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
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
The jar utility is used to create JAR files jar cfm <jarfile> <manifest> <files> 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
To bundle files into a JAR file: jar cfmMyDialog.jar manifest.txt modalDialog/DialogApp.classmodalDialog/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.txtis 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
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
Online tutorial • “Packaging Programs in JAR files” • http://java.sun.com/docs/books/tutorial/deployment/jar/index.html SE-2030 Dr. Rob Hasker