IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

[newInstance()] ClassCastException incompréhensible


Sujet :

Langage Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Février 2007
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2007
    Messages : 64
    Points : 54
    Points
    54
    Par défaut [newInstance()] ClassCastException incompréhensible
    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:
    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();
    			}
    Le problème se trouve à cette ligne:
    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:
    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)
    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.

    Voici le code de l'interface et de la class que j'essaie d'instancier:
    interface "PluginBase.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();
    }
    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
    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
     
    	}
     
    }

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Février 2007
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2007
    Messages : 64
    Points : 54
    Points
    54
    Par défaut
    j'ai trouvé ...


    en plus, je m'en doutais ...



    une simple histoire de ... class en double ... 2 versions différentes .... problème survenu a cause d'un autre problème : j'ai déplacé des class d'un projet à un autre, lors de la suppression, les sources sont partie mais pas les binaires ... j'avais donc un PluginBase (l'interface) en double et pas la meme version d'un module web à l'autre .... problème classique quoi ...

    Merci de pas m'avoir répondu, ca n'en valais pas la peine



+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. messages incompréhensibles
    Par devalender dans le forum Outils
    Réponses: 2
    Dernier message: 06/07/2004, 16h53
  2. [Reflection] Probleme d'appel newInstance()
    Par SEMPERE Benjamin dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 17/06/2004, 13h15
  3. incompréhension avec ado
    Par Orgied dans le forum Bases de données
    Réponses: 3
    Dernier message: 19/05/2004, 18h24
  4. [WSAD5] probleme incompréhensible
    Par capitaine_banane dans le forum Eclipse Java
    Réponses: 5
    Dernier message: 07/04/2004, 11h56
  5. [JSP] Erreur incompréhensible
    Par xxaragornxx dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 09/09/2003, 16h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo