从不同的JAR调用具有相同packageName的相同方法 [英] Calling same Method having same packageName From Different JARs

查看:84
本文介绍了从不同的JAR调用具有相同packageName的相同方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个Jar文件。所有jar文件都包含相同的类TestServicesImpl和相同的方法displayWeLcomeMessage(),但是displayWeLcomeMessage()的消息(输出)不同。
示例:

I have three Jar files.All jar files contain Same class TestServicesImpl And Same Method displayWeLcomeMessage() But having different messages(output) of displayWeLcomeMessage(). Example :

public void displayWeLcomeMessage() {
        System.out.println("wecome msg of JAR version first");

    }

 public void displayWeLcomeMessage() {
            System.out.println("wecome msg of JAR version two");

        }

 public void displayWeLcomeMessage() {
            System.out.println("wecome msg of JAR version third");

        }

我有一个主应用程序,其中包含jar。我的主要应用程序调用displayWeLcomeMessage()方法。

I have One main application and it contains jars included. My main application calls displayWeLcomeMessage() method.

将第一个JAR添加到类路径中,然后将第二个JAR加载到自定义类加载器中,并调用方法displayWeLcomeMessage()。

first JAR is added in classpath and second JAR is loaded with custom classloader and invoke method displayWeLcomeMessage().

    File file  = new File("C:/Users/amitk/Desktop/Test_1.0.2.jar");
    @SuppressWarnings("deprecation")
    URL url = file.toURL();  
    URL[] urls = new URL[]{url};
    URLClassLoader  loader = new URLClassLoader(urls);

    Class classS = loader.loadClass("com.amit.servicesImpl.TestServicesImpl");
    Object object = classS.newInstance();
    Method getmsg = classS.getMethod("displayWeLcomeMessage");
     getmsg.invoke(object);

,但它显示的消息与JAR方法相同。
在我的第三个JAR中,我更改了程序包名称。

com.amit.servicesImpl.TestServicesImpl 更改为 com.amit.servicesImpl2.TestServicesImpl
,这一次可以正常工作正确地显示了JAR 3方法的消息。

but it displays the same message as in method of JAR first. In my third JAR, i have changed the package name. that is com.amit.servicesImpl.TestServicesImpl is changed to com.amit.servicesImpl2.TestServicesImpl and this time it works properly that is message of method of JAR 3 is displayed here.

所以让我知道其背后的主要问题以及解决方案。

so let me know the main issue behind this.and solution for this.

推荐答案

也许您的初始类加载器中有JAR。
URLClassLoader将在检查自己的空间之前检查父类加载器中的现有类。

Maybe you have your JAR in your initial class loader. URLClassLoader will check existing class in parent class loader before checking in its own space.

1)您可以扩展和修改此行为:

1) You can extend and modify this behavior:

package com.mytool;

import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandlerFactory;
import java.util.HashMap;
import java.util.Map;

public class MyURLClassLoader extends URLClassLoader {

    private final Map<String, Class<?>> ourClasses = new HashMap<>();

    public MyURLClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }

    public MyURLClassLoader(URL[] urls) {
        super(urls);
    }

    public MyURLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
        super(urls, parent, factory);
    }

    @Override
    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = ourClasses.get(name);

            if (c == null) {
                // search in our paths
                try {
                    c = findClass(name);
                    ourClasses.put(name, c);
                } catch (ClassNotFoundException e) {
                    // ignore
                }
            }

            if (c == null) {
                c = findLoadedClass(name);
            }

            if (c != null) {
                if (resolve) {
                    resolveClass(c);
                }
                return c;
            }

            // default search
            return super.loadClass(name, resolve);
        }
    }
}

2)或者您可以尝试来移动我们的JAR而不是在JVM启动时加载它。

2) Or you can try to move our JAR and not load it at JVM start.

注意:
我将不使用完全自反性使用仅由初始类加载器加载的接口
。您的对象可以实现它,并且可以强制转换为该接口。如果您使用MyURLClassLoader进行此操作,请不要将此接口添加到动态加载的JAR中!

Note: Instead of using a full reflexivity, I'll use an interface loaded only by the initial classloader. Your object could implements it, and you'll be able to cast to this interface. If you do this with MyURLClassLoader, please don't add this interface in our dynamic loaded JAR!

这篇关于从不同的JAR调用具有相同packageName的相同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆