简体   繁体   English

Java中的自定义ClassLoader,用于从hdfs加载jar

[英]Custom ClassLoader in java for loading jar from hdfs

I want to load a jar from HDFS using custom java URL ClassLoader in my program. 我想在程序中使用自定义Java URL ClassLoader从HDFS加载jar。 I could not find examples on internet. 我在互联网上找不到示例。 I have seen examples that load jars from local file system as mentioned in following thread: 我看到了以下线程中提到的从本地文件系统加载jar的示例:

Custom URLClassLoader 自定义URLClassLoader

Any Suggestions or working example? 有什么建议或可行的例子吗?

Thanks. 谢谢。

this is how you can write your custom class loader. 这就是编写自定义类加载器的方法。 the code is written in scala. 该代码是用scala编写的。 you can convert it into java . 您可以将其转换为java。

class HdfsClassLoaderclassLoader(classLoader: ClassLoader) extends URLClassLoader(Array.ofDim[URL](0), classLoader) {

    def addJarToClasspath(jarName: String) {
        synchronized {
            var conf = new Configuration
            val fileSystem = FileSystem.get(conf)
            val path = new Path(jarName);
            if (!fileSystem.exists(path)) {
                println("File does not exists")
            }
            val uriPath = path.toUri()
            val urlPath = uriPath.toURL()
            println(urlPath.getFile)
            addURL(urlPath)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM