public static void main(String[] args) throws Exception{ String classPath = "com.xxx.flink.udx.TimeFormatUDF"; ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader(); DtClassLoader dtClassLoader = new DtClassLoader(new URL[]{}, threadClassLoader); List<URL> jarURList = Arrays.asList(new URL("file://C:\\Work\\liulanqixiazai\\pangolin-assembly-0.1-20190919.jar")); //FlinkUtil.loadExtraJar(jarURList, dtClassLoader); dtClassLoader.addURL(new URL("file://C:\\Work\\liulanqixiazai\\pangolin-assembly-0.1-20190919.jar")); // dtClassLoader.getClassLoadingLock(name) dtClassLoader.loadClass(classPath); ScalarFunction udfFunc = Class.forName(classPath, false, dtClassLoader) .asSubclass(ScalarFunction.class).newInstance(); }
删除代码报错:
Exception in thread "main" java.lang.ClassNotFoundException: com.didachuxing.flink.udx.TimeFormatUDF
at com.dtstack.flink.sql.classloader.DtClassLoader.loadClass(DtClassLoader.java:139)
at com.dtstack.flink.sql.classloader.DtClassLoader.loadClass(DtClassLoader.java:71)
at com.dtstack.flink.sql.util.Test.main(Test.java:25)
原因:
windows文件目录写错
解决方法:
注意在本地windows上写文件目录时,file:后面需要一个"/"而不是两个.
将这一句修改为
dtClassLoader.addURL(new URL("file:/C:\\Work\\liulanqixiazai\\pangolin-assembly-0.1-20190919.jar"));
即可