来自远程目录的 Java Class.forName() [英] Java Class.forName() from distant directory

查看:35
本文介绍了来自远程目录的 Java Class.forName()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Class.forName() 加载 Java 类来加载它.

I am currently loading Java classes using Class.forName() to load it.

clazz = Class.forName("interfaces.MyClass");

但现在我想从不同的目录加载类,我试图通过

But now I want to load classes from different directory, I have tried to set classpath by

clazz = Class.forName("-cp "C:/dir" distantinterfaces.DistantClass");

没有成功和 ClassNotFoundException.远程类的完整路径是:

With no success and ClassNotFoundException. Full path to distant class is:

C:/dir/distantinterfaces/DistantClass.class

推荐答案

使用 URLClassLoader 为此.代码可能类似于:

Use an URLClassLoader for this. The code might be something along the lines of:

File f = new File("C:/dir");
URL[] cp = {f.toURI().toURL()};
URLClassLoader urlcl = new URLClassLoader(cp);
Class clazz = urlcl.loadClass("distantinterfaces.DistantClass");

这篇关于来自远程目录的 Java Class.forName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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