-
1. Re: Loading class files at runtime
wdfink Apr 1, 2014 2:23 AM (in response to aanshu_2302)Could you show an example how you package the application and how your code look like?
-
2. Re: Loading class files at runtime
aanshu_2302 Apr 1, 2014 3:12 AM (in response to wdfink)I am passing my entities packagename.
private static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
for (File directory : dirs) {
classes.addAll(findClasses(directory, packageName));
}
System.out.println("Classes : " + classes);
return classes.toArray(new Class[classes.size()]);
}
Relevant code is attached.
My ear file has ejb.jar and 2 war files.
-
code.txt.zip 1.0 KB
-
-
3. Re: Loading class files at runtime
wdfink Apr 1, 2014 5:34 AM (in response to aanshu_2302)File access will be not supported for EE applications, there might be issues as you see here.
You might use the same class loader from a class inside your application, not the current contextCL.
Also you need to consider that you might have restrictions with war files, see Class Loading in AS7
-
4. Re: Loading class files at runtime
aanshu_2302 Apr 1, 2014 10:28 AM (in response to wdfink)Could you tell me what all needs to be done if I have to do this.
I have a requirement to read class metadata of all my entities and store it during server startup. I have a Singleton class for that.
Please suggest.
-
5. Re: Loading class files at runtime
aanshu_2302 Apr 5, 2014 3:55 AM (in response to aanshu_2302)I have solved the problem by reading metadata directly from Entitymanager itself.
Earlier, I had wanted to avoid this as I thought maybe reading the class files directly would be better off than just reading those from EntityManager but nevertheless went ahead with Entitymanager approach.