Monday 10 June 2019

Loading a class using URLClassLoader continuation


This is continuation to my previous post. In this post, I am going to create two jar files.
a.   interfaces.jar
b.   implementation.jar




Creating interfaces.jar file
Create Arithmetic.java file by following below directory structure.
$tree
.
└── interfaces
    └── com
        └── sample
            └── demoapp
                └── interfaces
                    └── Arithmetic.java

5 directories, 1 file
$


Arithmetic.java
package com.sample.demoapp.interfaces;

public interface Arithmetic {

 public int add(int a, int b);
 
 public int sub(int a, int b);
 
 public int mul(int a, int b);
 
 public int div(int a, int b);
}


Go inside of interfaces folder and compile Arithmetic.java.
$cd interfaces/
$
$javac com/sample/demoapp/interfaces/Arithmetic.java 
$
$tree
.
└── com
    └── sample
        └── demoapp
            └── interfaces
                ├── Arithmetic.class
                └── Arithmetic.java

4 directories, 2 files

Create jar file by executing below command from interfaces folder.

jar -cvf interfaces.jar *

$jar -cvf interfaces.jar *
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/sample/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/interfaces/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/interfaces/Arithmetic.class(in = 195) (out= 157)(deflated 19%)
adding: com/sample/demoapp/interfaces/Arithmetic.java(in = 205) (out= 102)(deflated 50%)


Total project structure looks like below.
$tree
.
└── interfaces
    ├── com
    │   └── sample
    │       └── demoapp
    │           └── interfaces
    │               ├── Arithmetic.class
    │               └── Arithmetic.java
    └── interfaces.jar

5 directories, 3 files

Step 2: Create implementation.jar file.


Create ArithemticImpl.java by following below directory structure.
$tree
.
├── implementation
   └── com
       └── sample
           └── demoapp
               └── interfaces
                   └── impl
                      └── ArithemticImpl.java


ArithemticImpl.java
package com.sample.demoapp.interfaces.impl;

import com.sample.demoapp.interfaces.Arithmetic;

public class ArithmeticImpl implements Arithmetic {

 @Override
 public int add(int a, int b) {
  return a + b;
 }

 @Override
 public int sub(int a, int b) {
  return a - b;
 }

 @Override
 public int mul(int a, int b) {
  return a * b;
 }

 @Override
 public int div(int a, int b) {
  return a / b;
 }

}


Total directory structure of interfaces and implementation project looks like below.
$tree
.
├── implementation
│   └── com
│       └── sample
│           └── demoapp
│               └── interfaces
│                   └── impl
│                       └── ArithemticImpl.java
└── interfaces
    ├── com
    │   └── sample
    │       └── demoapp
    │           └── interfaces
    │               ├── Arithmetic.class
    │               └── Arithmetic.java
    └── interfaces.jar

11 directories, 4 files

Go to implementation directory and compile ArithmeticImpl.java file by executing below command.


javac -cp ../interfaces/ com/sample/demoapp/interfaces/impl/ArithmeticImpl.java

$javac -cp ../interfaces/ com/sample/demoapp/interfaces/impl/ArithmeticImpl.java
$
$tree
.
└── com
    └── sample
        └── demoapp
            └── interfaces
                └── impl
                    ├── ArithmeticImpl.class
                    └── ArithmeticImpl.java

5 directories, 2 files


Create jar file by executing below command from the implementation folder.
$jar -cvf implementation.jar *
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/sample/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/interfaces/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/interfaces/impl/(in = 0) (out= 0)(stored 0%)
adding: com/sample/demoapp/interfaces/impl/ArithmeticImpl.java(in = 403) (out= 158)(deflated 60%)
adding: com/sample/demoapp/interfaces/impl/ArithmeticImpl.class(in = 485) (out= 272)(deflated 43%)
$
$tree
.
├── com
│   └── sample
│       └── demoapp
│           └── interfaces
│               └── impl
│                   ├── ArithmeticImpl.class
│                   └── ArithmeticImpl.java
└── implementation.jar

5 directories, 3 files

Step 3: Create App.java that Load ArithmeticImpl class using classloader.

Create App.java file, where exactly interface and implementation folders are located.


App.java
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

import com.sample.demoapp.interfaces.Arithmetic;

public class App {

 public static void main(String args[])
   throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
  URL url = new URL(
    "file:/Users/krishna/Documents/TechnicalDocuments/Java/examples/classloading/implementation/implementation.jar");

  URLClassLoader classLoader = new URLClassLoader(new URL[] { url });
  Class clazz = classLoader.loadClass("com.sample.demoapp.interfaces.impl.ArithmeticImpl");

  Arithmetic arithmetic = (Arithmetic) clazz.newInstance();

  System.out.println("Sum of 10 and 20 is " + arithmetic.add(10, 20));
  System.out.println("Subtraction of 10 and 20 is " + arithmetic.sub(10, 20));
  System.out.println("Multiplication of 10 and 20 is " + arithmetic.mul(10, 20));
  System.out.println("Division of 10 and 20 is " + arithmetic.div(10, 20));
 }
}

$tree
.
├── App.java
├── implementation
│   ├── com
│   │   └── sample
│   │       └── demoapp
│   │           └── interfaces
│   │               └── impl
│   │                   ├── ArithmeticImpl.class
│   │                   └── ArithmeticImpl.java
│   └── implementation.jar
└── interfaces
    ├── com
    │   └── sample
    │       └── demoapp
    │           └── interfaces
    │               ├── Arithmetic.class
    │               └── Arithmetic.java
    └── interfaces.jar

11 directories, 7 files

Compile App.java by executing below command.
javac -cp ./interfaces App.java

Execute App.java by running below command.

java -cp ./interfaces/: App
$java -cp ./interfaces/: App
Sum of 10 and 20 is 30
Subtraction of 10 and 20 is -10
Multiplication of 10 and 20 is 200
Division of 10 and 20 is 0

You can get the total working project code from below github location.



Previous                                                 Next                                                 Home

No comments:

Post a Comment