का उपयोग कर कक्षा का उदाहरण बनाते समय, यह जावा 1.4 (प्रोजेक्ट प्रतिबंध) है। मैं एक एप्लिकेशन मैनेजर बनाने की कोशिश कर रहा हूं। यह कस्टम क्लासलोडर के अपने उदाहरण का उपयोग करके प्रत्येक एप्लिकेशन की मुख्य कक्षा को लोड करता है। उसके बाद, यह प्रतिबिंब का उपयोग करके मुख्य वर्ग का एक उदाहरण बनाता है। प्रत्येक एप्लिकेशन एक सामान्य इंटरफ़ेस लागू करता है ताकि इंस्टेंस बनने के बाद, यह एप्लिकेशन की पूर्वनिर्धारित विधि चलाता है।क्लासकास्ट अपवाद जब प्रतिबिंब और क्लासलोडर
हालांकि, मुझे CRASH POINT 1 (कोड देखें) में कुछ परेशानी हो रही है। कक्षा को इसके इंटरफ़ेस के कार्यान्वयन के रूप में पहचाना नहीं गया है। यदि मैं इस कोड खंड को संगत करता हूं, तो मुझे CRASH POINT 2 पर क्लासकास्ट एक्सेप्शन मिलता है।
मुझे लगता है कि दोनों त्रुटियां एक ही समस्या से संबंधित हैं (बेशक)।
क्या कोई मेरी मदद कर सकता है? कोड का प्रासंगिक भाग निम्नानुसार है (आयात हटा दिए जाते हैं) ...
बहुत बहुत धन्यवाद।
मार्कस
// AppManager.java
public class AppManager {
public ThreadGroup threadGroup;
private Class appClass;
private AppInstance appInst;
public AppContextImpl context;
private AppManager(CustomClassLoader cl, String mainClass) throws ClassNotFoundException {
final String className = mainClass;
final CustomClassLoader finalLoader = cl;
appClass = cl.loadClass(mainClass);
// DEBUG CODE:
Class[] k1 = AppInstance.class.getInterfaces();
System.out.println(k1.length + " interfaces for AppInstance.class:");
for (int ii = 0; ii < k1.length; ii++) {
System.out.println(" " + ii + " - " + k1[ii].getName() + " (" + k1[ii].getClassLoader() + ")");
}
Class[] k2 = appClass.getInterfaces();
System.out.println(k2.length + " interfaces for appClass instance:");
for (int ii = 0; ii < k2.length; ii++) {
System.out.println(" " + ii + " - " + k2[ii].getName() + " (" + k2[ii].getClassLoader() + ")");
}
// CRASH POINT 1
if (!(AppInstance.class.isAssignableFrom(appClass))) {
throw new IllegalArgumentException("Attempt to run a non-AppInstance class: " + appClass);
}
context = new AppContextImpl(mainClass, this);
cl.setAppManager(this);
Constructor m;
try {
m = appClass.getConstructor(new Class[0]);
// CRASH POINT 2
appInst = (AppInstance) m.newInstance(new Object[0]);
appInst.init(context);
} catch (Exception e) {
System.out.println("Got ClassCastException here!\n");
e.printStackTrace();
}
}
public static void main(String[] args) {
App app1;
String path1 = "/home/user/workspace/MultiTaskTest/bin/";
String app1Name = "App1";
Vector v1 = new Vector();
try {
v1.add(new URL(path1));
} catch (MalformedURLException e1) {
final File file1 = new File(path1);
try {
URL path1aux = (URL) AccessController.doPrivileged(
new PrivilegedExceptionAction() {
public Object run() throws IOException {
if (!file1.exists()) {
System.out.println("Warning: \"" + file1.getPath() + "\" not found");
return null;
}
return file1.toURI().toURL();
}
});
if (path1aux != null) {
v1.add(path1aux);
}
} catch (PrivilegedActionException e) {
e.getException().printStackTrace();
}
}
final URL[] array1 = (URL[]) v1.toArray(new URL[v1.size()]);
CustomClassLoader cl1 = (CustomClassLoader) AccessController.doPrivileged(
new PrivilegedAction() { public Object run() {
return new CustomClassLoader(array1);
}});
System.out.println("ClassLoader 1 created: " + cl1);
try {
app1 = new App(cl1, app1Name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Cannot find class App1.");
}
}
}
// AppInstance.java
public interface AppInstance {
public void init(ContextImpl context);
}
// App1.java
public class App1 implements AppInstance {
private AppContextImpl contextObj;
public void init(AppContextImpl context) {
this.contextObj = context;
System.out.println("Running App1...");
}
}
// AppContextImpl.java
public class AppContextImpl {
public String mainClass;
public AppManager app;
public AppContextImpl(String mainClass, AppManager app) {
this.mainClass = mainClass;
this.app = app;
}
}
// CustomClassLoader.java
public class CustomClassLoader extends URLClassLoader {
AppManager appInst;
public CustomClassLoader(URL[] paths) { super(paths, null); }
public void setAppManager(AppManager app) { this.appInst = app; }
}
AppManager.java फ़ाइल में डिबग कोड के लिए उत्पादन होता है:
0 interfaces for AppInstance.class:
1 interfaces for appClass instance:
0 - AppInstance ([email protected])
लगता है यह एक वर्ग लोडर मुद्दा है की तरह की जरूरत है। आपको यह सुनिश्चित करना होगा कि जिस वर्ग को आप लोड करते हैं, साथ ही ऐप इंस्टेंस इंटरफेस को उसी वर्ग लोडर से लोड किया गया था, अन्यथा जावा उन्हें पूरी तरह से असंबंधित मानता है। –
क्लास.getGenericInterfaces() और Class.getInterfaces() का आउटपुट क्या है? – TheLQ
लॉर्ड। क्वाकस्टार: प्रश्न फिर से जांचें।मैंने आपके द्वारा मांगी गई जानकारी प्रदान करने के लिए कुछ टिप्पणियां डाली हैं। – Marcus