java-来自另一个目录的类的初始包 [英] java - Initial package of class from another directory

查看:51
本文介绍了java-来自另一个目录的类的初始包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从另一个目录的类文件中初始打包时遇到了一些问题

I have some problem how to initial package from class file from another directory

File file=new File("D:\\java\\myproject\\Name_pack\\time\\MyClass.class");
URL[] cp =
{
    new File(file.getParent()).toURI().toURL()
};
URLClassLoader urlcl = new URLClassLoader(cp);
Class cls = urlcl.loadClass(file.getName().replaceAll("\\.class","");

如果类文件不包含软件包,则可以使用

但它包含软件包,我会遇到类似这样的错误:

if class file not contain a package, it's working.
but it's contains a package, i get some error like this :

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Waktu (wrong name: Name_pack/time/MyClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

我不知道如何获取包裹。

I don't know how to get package.

所以,我想要这样的初始字母:

so, i want initial like this :

Class cls = urlcl.loadClass(Name_pack.time.MyClass);


推荐答案

如果有的话,绝对是没有关于代码库或类的预期软件包的知识会使事情变得复杂。实际的全限定名称包含在类文件中,这就是类加载器拒绝它的原因;它甚至会在异常消息中告诉您实际的名称,但是,不能保证异常消息中将包含该信息。

If all you have, is an absolute file path without knowledge about the code base nor the expected package of the class, things get complicated. The actual full qualified name is contained in the class file, that’s why the class loader rejects it; it even tells you the actual name in the exception message, however, there is no guaranty that the exception’s message will contain that information.

由于我不认为您要为了开始解析字节码以找到实际的类名,我建议重新设计应用程序,以便首先分别接收类路径目录(代码库)和完整的合格类名。

Since I do not assume that you want to start parsing the byte code to find the actual class name, I suggest to redesign the application to receive the class path directory (code base) and full qualified class name separately in the first place.

一个简单但并不真正推荐的可能性就是尝试一下:

A simple, but not really recommended, possibility is just trying it:

File file=new File("D:\\java\\myproject\\Name_pack\\time\\MyClass.class");
String className=file.getName();
if(!className.endsWith(".class"))
    throw new IllegalArgumentException(file.toString());
className=className.substring(0, className.lastIndexOf('.'));
File codeBase=file;
Class cls = null;
do {
    codeBase=codeBase.getParentFile();
    URL[] cp = { codeBase.toURI().toURL() };
    URLClassLoader urlcl = new URLClassLoader(cp);
    try { cls = urlcl.loadClass(className); }
    catch(NoClassDefFoundError ex) {
        className=codeBase.getName()+'.'+className;
    }
} while(cls==null);

但是,正如所说的,最好的选择是让任何提供您文件的源,提供正确的代码库目录放在首位……

But as said, the best option is to let, whatever source provided you the file, provide the correct code base directory in the first place…

这篇关于java-来自另一个目录的类的初始包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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