Bonjour,
J'ai un petit problème. J'ai un code java qui doit chercher les class implémentant une certaine interface; une fois trouvé, il faut instancier cette class et l'utiliser via l'interface qu'elle implémante.
Voici le code:Le problème se trouve à cette ligne:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 try { //tmp_class = Class.forName(tmp_string, true, loader); tmp_class=Class.forName(tmp_string); for(int j=0; j<tmp_class.getInterfaces().length; j ++ ) { if(tmp_class.getInterfaces()[j].getName().toString().equals(PluginBase.class.getName()) ) { System.out.println("TROUVEEEEEEE: "+tmp_string); //allPluginsBase.add((PluginBase)tmp_class.newInstance()); PluginBase base; base=(PluginBase)tmp_class.newInstance(); System.out.println("HHH="+base.getName()); break; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part base=(PluginBase)tmp_class.newInstance();
Pour plus d'explication: je suis dans JBoss, c'est une Servlet qui exécute le code ci-dessus (enfin, un objet qui a été créé dans une servlet). Les class cherchée sont copié alors que l'application est déjà déployée (mais, pas de classdefnotfound donc... pas de problème; de plus, j'ai redémarrer le serveur d'application: aucun changement donc le problème ne provient pas de la).
Voici l'erreur exacte:
Comme vous pouvez le constater, une des deux class présente contient bien une implémentation de l'interface. Donc, je ne comprend pas pourquoi ce classcastexception apparait.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 16:54:07,837 INFO [STDOUT] IL Y A 2 fichier class 16:54:07,885 INFO [STDOUT] A= /home/loopx/workspace/workspace_PiX-Mania/base/outils/jboss-4.2.2.GA/server/default/deploy/PiX-MANIA.ear/lib_plugin_installed.jar/be/loopx/pixmania/helloworld/HelloServlet.class 16:54:07,886 INFO [STDOUT] B= /home/loopx/workspace/workspace_PiX-Mania/base/outils/jboss-4.2.2.GA/server/default/deploy/PiX-MANIA.ear/lib_plugin_installed.jar/be/loopx/pixmania/helloworld/HelloServlet 16:54:07,887 INFO [STDOUT] C= be.loopx.pixmania.helloworld.HelloServlet 16:54:07,894 INFO [STDOUT] A= /home/loopx/workspace/workspace_PiX-Mania/base/outils/jboss-4.2.2.GA/server/default/deploy/PiX-MANIA.ear/lib_plugin_installed.jar/be/loopx/pixmania/helloworld/HelloWorldPlugin.class 16:54:07,895 INFO [STDOUT] B= /home/loopx/workspace/workspace_PiX-Mania/base/outils/jboss-4.2.2.GA/server/default/deploy/PiX-MANIA.ear/lib_plugin_installed.jar/be/loopx/pixmania/helloworld/HelloWorldPlugin 16:54:07,896 INFO [STDOUT] C= be.loopx.pixmania.helloworld.HelloWorldPlugin 16:54:07,902 INFO [STDOUT] TROUVEEEEEEE: be.loopx.pixmania.helloworld.HelloWorldPlugin 16:54:07,903 ERROR [[PluginServlet]] "Servlet.service()" pour la servlet PluginServlet a lancé une exception java.lang.ClassCastException: be.loopx.pixmania.helloworld.HelloWorldPlugin at be.loopx.pixmania.plugin.PluginLoader.loadAllPluginBase(PluginLoader.java:100) at be.loopx.pixmania.servlet.PluginServlet.processRequest(PluginServlet.java:83) at be.loopx.pixmania.servlet.PluginServlet.doGet(PluginServlet.java:46) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292) at be.loopx.pixmania.servlet.ControlServlet.gotoPluginServlet(ControlServlet.java:217)
Voici le code de l'interface et de la class que j'essaie d'instancier:
interface "PluginBase.java"
class "HelloWorldPlugin.java"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 //by loopx //17/01/08 //define method which need to be implemented to become a plugin package be.loopx.pixmania.plugin; public interface PluginBase { public String getName(); public String getVersion(); public String getServletName(); public String getServletRelativeContext(); public void quickreminderProcess(); public void warningProcess(); public void mainProcess(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package be.loopx.pixmania.helloworld; import be.loopx.pixmania.plugin.PluginBase; public class HelloWorldPlugin implements PluginBase { public String getName() { return "Hello world"; } public String getServletName() { return HelloServlet.class.getName(); } public String getServletRelativeContext() { return "helloworld"; } public String getVersion() { return "0.1"; } public void mainProcess() { System.out.println(this.getClass().getName()+"> MAIN PROCESS!!!"); } public void quickreminderProcess() { // TODO Auto-generated method stub } public void warningProcess() { // TODO Auto-generated method stub } }
Partager