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

查看:624
本文介绍了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天全站免登陆