English 中文(简体)
2. 动态装饰java类卷宗的方法
原标题:Method to dynamically load java class files

动态装饰java族案卷的好办法是什么,这样,将编成册的方案能够读到所有类别档案中并使用这些档案,如何写这些档案,以便它们拥有与杰尔相关的必要的一揽子名称?

最佳回答

我认为,它有一份<条码>。

我建议你首先研究以下例子,即没有上班的班级文件。

// Create a File object on the root of the directory containing the class file
File file = new File("c:\myclasses\");

try {
    // Convert File to a URL
    URL url = file.toURI().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("com.mycompany.MyClass");
} catch (MalformedURLException e) {
} catch (ClassNotFoundException e) {
}
问题回答
Class myclass = ClassLoader.getSystemClassLoader().loadClass("package.MyClass");

Class myclass  = Class.f或Name("package.MyClass");

或 loading the class from different folder which is not in the classpath:

File f = new File("C:/dir");
URL[] cp = {f.toURI().toURL()};
URLClassLoader urlcl = new URLClassLoader(cp);
Class myclass  = urlcl.loadClass("package.MyClass");

F或 further usage of the loaded Class you can use Reflection if the loaded Class is not in your classpath and you can not imp或t and cast it. F或 example if you want to call a static method with the name "main":

Method m = myclass.getMethod("main", String[].class);
String[] args = new String[0];
m.invoke(null, args);  // invoke the method

如果你在课堂道路上添加一个目录,你可以在申请开始后增加课堂,这些课堂一旦被写到名录上即可装满。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签