简体   繁体   English

如何使用ClassLoader或Class以编程方式加载类

[英]How to use ClassLoader or Class to programatically load a Class

I have two classes 我有两节课

package pack2;
import java.lang.*;
import java.io.*;
class eg
{
    public void show()
    {
        System.out.println("ClassPath set to this class");
    }
}

this is in C:\\NNK\\pack2. 这是在C:\\ NNK \\ pack2中。 the fully qualified name is pack2.eg 完全限定名称为pack2.eg

another program is 另一个程序是

import java.io.*;
import java.lang.*;
import java.net.*;
class classload
{
    public static void main(String args[])
    {
        //have to load the eg class here. Dont know what i have done below is right or wrong
        try 
        {
            Class b=Class.forName("pack2.eg");
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        try
        {
            eg z=(eg) b.newInstance();
        }
        catch(InstantiationException l)
        {
            l.printStackTrace();
        }
        z.show();
        System.out.println("b.getName()="+b.getName());
    }
}

this program is at C:\\NNK I have to load the eg program here. 这个程序在C:\\ NNK我必须在这里加载eg程序。 I tried to learn it in Oracle saw other related stack overflow questions of it. 我试图在Oracle中学习它,看到了其他相关的堆栈溢出问题。 Dynamically loading a class in Java But it didn't work i keep getting the error b is Unknown and z is unknown symbol. 用Java动态加载类但是它不起作用我不断得到错误b是Unknown而z是未知符号。 Also is there a way to use the directory filename(eg: C:\\NNK\\pack2\\eg) to load a class 还有一种方法可以使用目录文件名(例如:C:\\ NNK \\ pack2 \\ eg)来加载一个类

In addition to multiple errors already identified by Jim Garrison answer, you've declared your class with "default" scope. 除了已经由Jim Garrison回答确定的多个错误之外,您已经使用“默认”范围声明了您的类。 This means it can only be referenced via classes in the same package. 这意味着它只能通过同一个包中的类引用。

should be 应该

public class eg   // <== public
{
    public void show()
    {
        System.out.println("ClassPath set to this class");
    }
}

And no need for separate try catches, why not just throw from the main method... If any step failed, eg defining b, you couldn't do anything more anyway... 并且不需要单独的尝试捕获,为什么不从主方法抛出...如果任何步骤失败,例如定义b,你无论如何都不能做任何事情......

public static void main(String[] args) throws Exception {

    Class b = Class.forName("pack2.eg");
    eg z = (eg)b.newInstance();
    z.show();

    System.out.println("b.getName()=" + b.getName());
}

This is a scope issue. 这是一个范围问题。 Move the declarations of b and z outside the first try , as in: 在第一次try之外移动bz的声明,如下所示:

public static void main(String args[])
{
    //have to load the eg class here. Dont know what i have done below is right or wrong
    Class b;
    eg z;
    try 
    {
        b=Class.forName("pack2.eg");
    }
    catch(ClassNotFoundException e)
    {
        e.printStackTrace();
    }
    try
    {
        z=(eg) b.newInstance();
    }
    catch(InstantiationException l)
    {
        l.printStackTrace();
    }
    z.show();
    System.out.println("b.getName()="+b.getName());
}

You will probably have to make pack2.eg public as well 您可能还必须public pack2.eg

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

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