Hi,

Optional packages feature of WebLogic allows us to easily share a single JAR file among multiple applications.

When to Use it?
Secnario: Third-party Web Application Framework classes needed by multiple Web applications can be packaged and deployed in a single JAR file, and referenced by multiple Web application modules in the domain.
1. Optional packages are delivered as basic JAR files that have no deployment descriptors. You simply designate the JAR as an optional package at deployment time, and WebLogic Server registers the file with the target servers you select. After the optional package has been registered, you can then deploy J2EE modules and applications that reference the optional package in their MANIFEST.MF files.
2. Optional packages feature of WebLogic allows us to easily share a single JAR file among multiple applications. When to Use it? Third-party Web Application Framework classes needed by multiple Web applications can be packaged and deployed in a single JAR file, and referenced by multiple Web application modules in the domain.
3. Optional packages are delivered as basic JAR files that have no deployment descriptors. You simply designate the JAR as an optional package at deployment time, and WebLogic Server registers the file with the target servers you select. After the optional package has been registered, you can then deploy J2EE modules and applications that reference the optional package in their MANIFEST.MF files.

For more Informations please refer to : http://download.oracle.com/docs/cd/E15051_01/wls/docs103/deployment/understanding.html#wp1052671

A real time demo and TestCase here :  http://forums.oracle.com/forums/thread.jspa?threadID=2173886&tstart=0

Here i am going to demonstrate a Very Simple Step procedure to utilize Optional Packages in our applications.

Developing Shared Library

Step 1). Just write a Simple Java Program … Lets say “HelloWorld.java

package hello.pack;
public class  HelloWorld {
	public void HelloWorld() {
		System.out.println("Hello World!");
	}

	public String sayHello(String name) {
		System.out.println("nt sayHello(String) method of class HelloWorld Invoked... ");
		return "Hello Mr. "+name;
	}
}

Step2 ). Compile the Program as usual

D:/DELETE/OptionalPackageDemo>javac  -d  .  HelloWorld.java

Step 3). Now We need to provide the “META-INF/MANIFEST.MF” file before creating the JAR. So first of all In the Current Directory create a Folder with name “META-INF” and inside it create a File Named as “MANIFEST.MF” and then write the Extension-Name inside the file. Like Folliwing…

Manifest-Version: 1.0
Class-Path:
Extension-Name: HelloWorld
Step 4). Now Create a Jar of it Which Should contain “hello.pack.HelloWorld.class” and “META-INF/MANIFEST.MF”
D:/DELETE/OptionalPackageDemo> jar -cvfM  HelloWorld.jar  hello  META-INF  MANIFEST.MF

NOTE: Make sure that in above command you use the JAR flags as:
(c=create, v=verbose, f=file, Capital M=add our own MANIFEST.MF file and dont create a default one)

Step 5). Now Deploy this JAR file in WebLogic Server …It’s Very Simple Just Login to AdminConsole of WebLogic Server and then Click on Lock and Edit then Just deploy it as a library …Thats all.

Using the Optional Package “HelloWorld.jar” in our Application?

Just follow the below mentioned Steps:
Step 6). Develop one Web Application …with “index.jsp” page as mentioned below:

<%@ page import=”com.test.hello.*” %>
 <%
	 hello h = new hello();
 	h.HelloWorld();
 	out.println(“<h2> h.sayHelloWorld(Jack) : ” +h.sayHelloWorld(“Jack”));
 %>

Step 7). Provide the default “WEB-INF/web.xml” file…like

 <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” 
 			“http://java.sun.com/dtd/web-app_2_3.dtd”>
	<web-app>
	</web-app>

Step 8). Just Beside the “WEB-INF” Folder Create another Folder “META-INF” and then inside the META-INF folder create a file “MANIFEST.MF” with following entries…

Manifest-Version: 1.0
Class-Path:
Extension-List: hello
hello-Extension-Name: HelloWorld

Step 9). Now Create a WAR file which should include “index.jsp” file , “META-INF” and “WEB-INF” folders

D:/DELETE/TestWebApp> jar -cvfM  TestWebApp.war   META-INF   WEB-INF   index.jsp

NOTE: jar command has an Additional Flag this time

c = Create
v = Verbose
f = File
M = (UPPER CASE M) Means Dont add a Default MANIFEST.MF in the WAR file…Rather Add Our Own “MANIFEST.MF” file in the Archieve.

Step 10). Now Just deploy this Above WAR file in WebLogic Server and then See that You are able to access the Index.jsp Page, which is reffering to a Class file “hello.pack.HelloWorld” which is not at all available inside the WAR …But we are able to access it in our WebApplication…Because of WebLogic’s Optional Package Feature
.
.

Thanks WebLogic…
If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.