In Java, it is possible to “extend” your currently installed JRE in a way that your classes are loaded automatically thereby removing the need to specify them in the classpath. This is done using Java’s Extension Mechanism.
[wp_ad_camp_1]
Below is an image showing the general idea of the Extension Mechanism.
Extensions are Jar files
Creating extensions is easy. All you need to do is bundle your classes into a JAR file.
Our Classes
We begin with two files – TurretaExtMainClass.java and TurretaExtSomeOtherClass.java. Nothing fancy. Just a main class that uses another class.
[wp_ad_camp_2]
1 2 3 4 5 6 7 8 9 | package com.turreta.ext; public class TurretaExtMainClass { public static void main(String[] args) { TurretaExtSomeOtherClass o = new TurretaExtSomeOtherClass(); o.printSomething(); } } |
1 2 3 4 5 6 7 8 | package com.turreta.ext; public class TurretaExtSomeOtherClass { public void printSomething() { System.out.println("*** From new extension ***"); } } |
Create a Jar file
There are several ways to create jar files. Here, we’ll do the simplest and primitive way.
1 | jar cvf turreta-ext.jar -C /path-somewhere/classes . |
Note: this assumes that /path-somewhere/classes contains the com directory.
[wp_ad_camp_3]
I am using Java 8 for this. You can check if you are using the correct platform.
1 | javac -version |
This outputs
1 2 3 | java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode) |
So, finally out extension is turreta-ext.jar which we will drop into the ext directory in Java home directory.
Drop it in the ext directory
Okay, make sure you are in the correct Java home directory.
[wp_ad_camp_4]
Proceed to lib\ext directory under jre.
Test your App
[wp_ad_camp_5]
You’ll notice my command prompt is “ms-dos:”. I changed it to keep things short.
Just type:
1 | prompt ms-dos: |