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. the fully qualified name is 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. I tried to learn it in Oracle saw other related stack overflow questions of it. Dynamically loading a class in Java But it didn't work i keep getting the error b is Unknown and z is unknown symbol. Also is there a way to use the directory filename(eg: C:\NNK\pack2\eg) to load a class
Aucun commentaire:
Enregistrer un commentaire