我想通过使用读取其源文件来获取java.lang.Class
a 的对象。class
FileReader
实际上,我想methods, constructors, parent class, overridden methods and imported packages
通过使用JFileChooser
. 所以,我想我通过使用它的类Class
对象方法getConstructors()
等得到了所有这些东西。
我试过这个,但它给了java.lang.ClassNotFoundException
......
public static void main(String[] args) {
File file = new File(
"F:\\study\\projects\\saralbhakti\\src\\signup\\SignupServlet.java");
try {
// Convert File to a URL
URL url = file.toURL(); // file:/c:/myclasses/
URL[] urls = new URL[] { url };
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// Load in the class; MyClass.class should be located in
// the directory file:/c:/myclasses/com/mycompany
Class cls = cl.loadClass("signup.SignupServlet");
System.out.println("Class Name : " + cls.getName());
Method[] declaredMethods = cls.getDeclaredMethods();
System.out.println("All Methods : " + declaredMethods.length);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}