Comments (6)
创建class v1版本jar包
- pom.xml
<groupId>com.github.charlemaznable</groupId>
<artifactId>same-name</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
- class
package com.github.charlemaznable.coexist;
public class SameName {
public String description() {
return "V0.0.1";
}
}
from blog.
创建class v2版本jar包
- pom.xml
<groupId>com.github.charlemaznable</groupId>
<artifactId>same-name</artifactId>
<version>0.0.2</version>
<packaging>jar</packaging>
- class
package com.github.charlemaznable.coexist;
public class SameName {
public String description() {
return "V0.0.2";
}
}
from blog.
分别打包, 将same-name-0.0.1.jar
与same-name-0.0.2.jar
置于目标工程的资源目录下:
|-- src
|-- main
|-- java
|-- resources
|-- same-name-0.0.1.jar
|-- same-name-0.0.2.jar
from blog.
创建包装类
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.
验证结果:
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.
from blog.
Related Issues (20)
- Maven的多继承 HOT 2
- 浅尝Java Instrumentation HOT 9
- SimpleDateFormat的隐藏技能
- 浅尝Cobra HOT 1
- 做一些摘抄 HOT 14
- 浅尝Kotlin HOT 3
- 领域实体类
- PECS - producer-extends, consumer-super HOT 5
- CircularFifoQueue vs EvictingQueue HOT 1
- 浅尝Hazelcast HOT 14
- 浅尝HyperLogLog HOT 6
- 信息熵 HOT 1
- Go 开发笔记 HOT 1
- 0x5f375a86
- 空方法编译报错?!
- Lombok it! HOT 4
- 注解到底是什么
- 缓存风险
- 日常学习笔记 2024
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.