Java jar seal packages

Filed Under: Java

Java JAR provides option to seal the package. Java jar seal package means that if any program is using that jar then all the classes should be loaded from that jar itself. Any attempt to load the classes from other jar files will throw java.lang.SecurityException. The documentation for sealing jar packages seems confusing, so we will learn this feature with sample programs.

Java jar seal packages

For effect of jar package sealing, we need at least two jar files to use in a test program. Below image shows two java project I have with same package names but with different classes.

Read following posts to know how to create jar from java classes and how to run jar file in java.

java jar seal

For sealing packages in a jar, we need to add it’s entries in jar manifest file. So I have the manifest file with following content.

manifest.txt


Name: com.jd.seal
Sealed: true

Now I run following commands in both projects to generate two jar files with above manifest entry.


pankaj@JD:~/CODE/seal1/bin$ jar cvmf manifest.txt seal1.jar com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/jd/(in = 0) (out= 0)(stored 0%)
adding: com/jd/seal/(in = 0) (out= 0)(stored 0%)
adding: com/jd/seal/A.class(in = 419) (out= 299)(deflated 28%)
pankaj@JD:~/CODE/seal1/bin$ cd ../../seal2/bin
pankaj@JD:~/CODE/seal2/bin$ jar cvmf manifest.txt seal2.jar com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/jd/(in = 0) (out= 0)(stored 0%)
adding: com/jd/seal/(in = 0) (out= 0)(stored 0%)
adding: com/jd/seal/B.class(in = 419) (out= 299)(deflated 28%)

Java jar seal packages effect

Now I will write a sample program that will use both these jar files to show the effect of java jar sealing.


import com.jd.seal.A;
import com.jd.seal.B;

public class MyClass{

	public static void main(String args[]){
	
		A a = new A();
		B b = new B();
	
	}
}

So MyClass is trying to load class A from jar file seal1.jar and class B from seal2.jar. Let’s try to compile and run this class and see what happens.


pankaj@JD:~/tmp$ javac -cp seal1.jar:seal2.jar MyClass.java 
pankaj@JD:~/tmp$ java -cp seal1.jar:seal2.jar:. MyClass
A class loaded
Exception in thread "main" java.lang.SecurityException: sealing violation: package com.jd.seal is sealed
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:234)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
	at MyClass.main(MyClass.java:9)

As you can see that it throws java.lang.SecurityException at runtime with error message as “package is sealed“.

Jar package sealing helps us in making sure that all the classes are loaded from same version of jar file.

Tip: If you want all the packages of a jar file to be sealed, you can just add entry for Sealed as true. To exclude any package from sealing, you can add an extra entry with Sealed as false.

MANIFEST.MF


Sealed: true

Name: com.jd.util
Sealed: false

That’s all about java jar sealing packages.

Reference: Oracle Doc

Comments

  1. Will G says:

    Thanks for the clear explanation and example!

  2. Alireza says:

    Thanks for sharing your knowledge. I have a question from you, when you said “from same version of jar file”, what is ‘same version’ mean? I think sealing packages in JAR files means there is no way to be used by another program. Am I wrong?

  3. jafar2049 says:

    It has been two days since I’ve been looking for a “SIMPLE” working example in order to understand the security idea behind the sealed jar files.

    Thank you very much for this nice explanation. Brilliant !

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages