Coder Social home page Coder Social logo

浅尝Java ClassLoader about blog HOT 6 OPEN

CharLemAznable avatar CharLemAznable commented on July 26, 2024
浅尝Java ClassLoader

from blog.

Comments (6)

CharLemAznable avatar CharLemAznable commented on July 26, 2024

创建class v1版本jar包

  1. pom.xml
<groupId>com.github.charlemaznable</groupId>
<artifactId>same-name</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
  1. class
package com.github.charlemaznable.coexist;

public class SameName {

    public String description() {
        return "V0.0.1";
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on July 26, 2024

创建class v2版本jar包

  1. pom.xml
<groupId>com.github.charlemaznable</groupId>
<artifactId>same-name</artifactId>
<version>0.0.2</version>
<packaging>jar</packaging>
  1. class
package com.github.charlemaznable.coexist;

public class SameName {

    public String description() {
        return "V0.0.2";
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on July 26, 2024

分别打包, 将same-name-0.0.1.jarsame-name-0.0.2.jar置于目标工程的资源目录下:

|-- src
    |-- main
        |-- java
        |-- resources
            |-- same-name-0.0.1.jar
            |-- same-name-0.0.2.jar

from blog.

CharLemAznable avatar CharLemAznable commented on July 26, 2024

创建包装类

package com.github.charlemaznable.coexist;

import lombok.AllArgsConstructor;
import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.File;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static com.github.charlemaznable.core.lang.ClzPath.classResource;

public class SameNameWrapper {

    private Object v1;
    private Object v2;

    @SneakyThrows
    public SameNameWrapper() {
        val sameNameClassName = "com.github.charlemaznable.coexist.SameName";

        try (val jarV1 = newJarFile("same-name-0.0.1.jar")) {
            val classV1 = classForNameInJarFile(sameNameClassName, jarV1);
            v1 = classV1.getConstructor().newInstance();
        }

        try (val jarV2 = newJarFile("same-name-0.0.2.jar")) {
            val classV2 = classForNameInJarFile(sameNameClassName, jarV2);
            v2 = classV2.getConstructor().newInstance();
        }
    }

    @SneakyThrows
    public String descriptionV1() {
        return (String) v1.getClass().getMethod("description").invoke(v1);
    }

    @SneakyThrows
    public String descriptionV2() {
        return (String) v2.getClass().getMethod("description").invoke(v2);
    }

    @SneakyThrows
    private JarFile newJarFile(String classPath) {
        return new JarFile(new File(classResource(classPath).getFile()));
    }

    @SneakyThrows
    private Class<?> classForNameInJarFile(String className, JarFile jarFile) {
        return Class.forName(className, true, new JarFileClassLoader(jarFile));
    }

    @Slf4j
    @AllArgsConstructor
    private static class JarFileClassLoader extends ClassLoader {

        private JarFile jarFile;

        @SneakyThrows
        @Override
        protected Class<?> findClass(String name) {
            val entryName = name.replaceAll("[.]", "/") + ".class";
            val entry = jarFile.getJarEntry(entryName);
            if (entry == null)
                throw new ClassNotFoundException("Jar entry not found");

            val data = new byte[(int) entry.getSize()];
            @Cleanup InputStream in = jarFile.getInputStream(entry);
            val result = in.read(data);
            if (result < 0) log.warn("read error");
            return defineClass(name, data, 0, data.length);
        }
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on July 26, 2024

验证结果:

package com.github.charlemaznable.coexist;

import lombok.val;

public class SameNameDemo {

    public static void main(String[] args) {
        val sameNameWrapper = new SameNameWrapper();
        System.out.println(sameNameWrapper.descriptionV1());
        System.out.println(sameNameWrapper.descriptionV2());
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on July 26, 2024

参考: java中两个出现两个完全相同的包名和类名解决方法

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.