Java Application Deployment: Packaging for External Execution
E N D
Presentation Transcript
Deploying Java applications as JAR files How to package an application so that you can run it outside of Eclipse. SE-2030 Dr. Mark L. Hornick
Consider an Eclipse project This application consists of files in two directories, in packages called demo.app and demo.data SE-2030 Dr. Mark L. Hornick
When you run an application from within Eclipse, it (internally) issues the following command: java –cp “D:\My Docs\se2030\JARDemo\bin” demo.app/JARDemoApp javais the command that runs the Java Virtual Machine • Found at C:\Program Files\Java\jdk1.6.0_03\bin\java.exe JARDemoApp is the name of the main class demo.app is the package containing the main class (demo.data is the name of the package containing the MyData class) -cp <classpath> specifies the base directory where the .class file(s) for the packages are located • Not including the package subdirectories • The classpath can be specified either relative or absolute SE-2030 Dr. Mark L. Hornick
If your class files are in multiple file folders, you must include them all: java –cp “D:\My Docs\se2030\JARDemo\bin; D:\MyDocs\Documents\MSOE\Courses\Example Programs\se2030\UIHelper\bin” demo.app/JARDemoApp -cp <classpath> specifies ALL base directories where the .class file(s) for the packages are located • Note that each directory is separated by a semicolon • With NO space between directories SE-2030 Dr. Mark L. Hornick
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. Mark L. Hornick
The jar utility can be used to create JAR files jar cfm <jarfile> <manifest> -C <bin> <classfiles> 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.-C <bin> specifies the location of the classfiles classfiles specifies the files you want to place in the JAR file • Separated by spaces • Must include the full package folder path SE-2030 Dr. Mark L. Hornick
To bundle files in the same directory into a JAR file: jar cfm JARDemoApp.jar manifest.txt –C ./bindemo/app/JARDemoApp.class–C ./bin demo/data/MyData.class This assumes: • you are issuing the jar command from within the directory of D:\My Docs\se2030\JARDemo • manifest.txtis in the same directory • Demo/app and demo/data are the names of the packages such that all class files are located in D:\My Docs\se2030\JARDemo\bin\demo\app or D:\My Docs\se2030\JARDemo\bin\demo\data • the capitalization of the class filename is IDENTICAL to the capitalization of the class itself (be careful on Windows because the OS is case-insensitive but Java is NOT) SE-2030 Dr. Mark L. Hornick
Manifest details Manifest.txtis an text file containing the following text: Manifest-Version: 1.0 Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.) Main-Class: demo.app.JARDemoApp SE-2030 Dr. Mark L. Hornick
To deploy your application, you just have to copy the JAR file to someplace (e.g. C:\temp) on the target PC To run the application bundled within a JAR file, issue the following command: java –jar “C:\temp\JARDemoApp.jar” Or create a shortcut containing the above command SE-2030 Dr. Mark L. Hornick
If your JAR file references other JAR files (like Helper.jar)… The Manifest.txtmust contain a reference to the location of the other JAR file(s): Manifest-Version: 1.0 Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.) Main-Class: demo.app.JARDemoApp Class-Path: ./Helper.jar Note that ./ specifies that Helper.jar in this case can be found in the same directory folder as JARDemoApp.jar (i.e. they are both in C:\temp). SE-2030 Dr. Mark L. Hornick
Online tutorial • “Packaging Programs in JAR files” • http://java.sun.com/docs/books/tutorial/deployment/jar/index.html SE-2030 Dr. Mark L. Hornick