日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線(xiàn)咨詢(xún)客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了如何在JDK11+中動(dòng)態(tài)加載運(yùn)行時(shí)的JAR文件?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)應(yīng)用程序,它使用以下解決方案在運(yùn)行時(shí)動(dòng)態(tài)加載JAR文件:

File file = ...
URL url = file.toURI().toURL();

URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);

這是使用How to load JAR files dynamically at Runtime?

的答案完成的

我現(xiàn)在想要一個(gè)適用于JDK11+的解決方案,與我使用的原始解決方案等價(jià)。因此,無(wú)需第三方庫(kù)/框架或加載/調(diào)用單個(gè)類(lèi)即可編程完成。

我嘗試了以下操作:

    創(chuàng)建了擴(kuò)展UrlClassLoader的DynamicClassLoader:
public final class DynamicClassLoader extends URLClassLoader {


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

    public DynamicClassLoader(String name, ClassLoader parent) {
        super(name, new URL[0], parent);
    }
    
    public DynamicClassLoader(ClassLoader parent) {
        this("classpath", parent);
    }

    public void addURL(URL url) {
        super.addURL(url);
    }
    
}
    然后我使用java.system.class.loader標(biāo)志啟動(dòng)我的應(yīng)用程序:

java -Djava.system.class.loader=com.example.DynamicClassLoader

然后我有一個(gè)JAR文件作為路徑對(duì)象,我使用以下方法調(diào)用它:

public void loadJar(Path path) throws Exception {
        
        URL url = path.toUri().toURL();
        
        DynamicClassLoader classLoader = (DynamicClassLoader)ClassLoader.getSystemClassLoader();
        Method method = DynamicClassLoader.class.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(classLoader, url);        
    }

調(diào)用此方法時(shí),我得到以下強(qiáng)制轉(zhuǎn)換類(lèi)異常:

class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class com.example.classloader.DynamicClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader is
 in module java.base of loader 'bootstrap'; com.example.classloader.DynamicClassLoader is in unnamed module of loader 'app')

我在OpenJDK11(內(nèi)部版本號(hào)11.0.10+9)上使用Spring Boot(2.4)。

推薦答案

根據(jù)評(píng)論區(qū)中的討論,我找到了解決方案。這可能不像我希望的那樣通用,并假設(shè)您知道要使用的類(lèi)(相反,只需將Maven依賴(lài)項(xiàng)添加到pom.xml或舊的JDK8解決方案中)。

    下載Maven依賴(lài)項(xiàng)(使用Jeka)

List<Path> paths = resolveDependency(groupId, artifactId, version);

public List<Path> resolveDependency(String groupId, String artifactId, String version) throws Exception {
        
        String dependency = groupId + ":" + artifactId + ":" + version;
        
        JkDependencySet deps = JkDependencySet.of()
                .and(dependency)
                .withDefaultScopes(COMPILE_AND_RUNTIME);

        JkDependencyResolver resolver = JkDependencyResolver.of(JkRepo.ofMavenCentral());
        List<Path> paths = resolver.resolve(deps, RUNTIME).getFiles().getEntries();

        return paths;

    }
    加載JAR文件

List<Class> classes = loadDependency(paths);

public List<Class> loadDependency(List<Path> paths) throws Exception {

        List<Class> classes = new ArrayList<>();

        for(Path path: paths){

            URL url = path.toUri().toURL();
            URLClassLoader child = new URLClassLoader(new URL[] {url}, this.getClass().getClassLoader());

            ArrayList<String> classNames = getClassNamesFromJar(path.toString());

            for (String className : classNames) {
                Class classToLoad = Class.forName(className, true, child);
                classes.add(classToLoad);
            }
        }

        return classes;

    }


    // Returns an arraylist of class names in a JarInputStream
    private ArrayList<String> getClassNamesFromJar(JarInputStream jarFile) throws Exception {
        ArrayList<String> classNames = new ArrayList<>();
        try {
            //JarInputStream jarFile = new JarInputStream(jarFileStream);
            JarEntry jar;

            //Iterate through the contents of the jar file
            while (true) {
                jar = jarFile.getNextJarEntry();
                if (jar == null) {
                    break;
                }
                //Pick file that has the extension of .class
                if ((jar.getName().endsWith(".class"))) {
                    String className = jar.getName().replaceAll("/", "\.");
                    String myClass = className.substring(0, className.lastIndexOf('.'));
                    classNames.add(myClass);
                }
            }
        } catch (Exception e) {
            throw new Exception("Error while getting class names from jar", e);
        }
        return classNames;
    }


// Returns an arraylist of class names in a JarInputStream
// Calls the above function by converting the jar path to a stream
private ArrayList<String> getClassNamesFromJar(String jarPath) throws Exception {
        return getClassNamesFromJar(new JarInputStream(new FileInputStream(jarPath)));
    }

    使用類(lèi)

就像雷納托指出的那樣,要知道您需要了解使用它的類(lèi)。在我的例子中,它是一個(gè)駱駝組件,我需要強(qiáng)制轉(zhuǎn)換并添加到這個(gè)框架中。類(lèi)是您在第二步中檢索到的類(lèi),方案是component的名稱(chēng)。


Component camelComponent = getComponent(classes, scheme);
context.addComponent(scheme, camelComponent);

public Component getComponent(List<Class> classes, String scheme) throws Exception {

        Component component = null;
        for(Class classToLoad: classes){
            String className = classToLoad.getName().toLowerCase();
            if(className.endsWith(scheme + "component")){
                Object object =  classToLoad.newInstance();
                component = (Component) object;
            }
        }

        return component;
    }

因此,第二部分是使其動(dòng)態(tài)可用。添加了第一部分和第三部分,以獲得完整解決方案的示例。

這篇關(guān)于如何在JDK11+中動(dòng)態(tài)加載運(yùn)行時(shí)的JAR文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:JAR JDK11 動(dòng)態(tài) 如何在 文件 行時(shí) 載運(yùn)
用戶(hù)無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定