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.


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 
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 Method)
	at java.lang.ClassLoader.loadClass(
	at sun.misc.Launcher$AppClassLoader.loadClass(
	at java.lang.ClassLoader.loadClass(
	at MyClass.main(

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.


Sealed: true

Name: com.jd.util
Sealed: false

That’s all about java jar sealing packages.

Reference: Oracle Doc


  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 !

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors