ERLEDIGT
JA
JA
ANTWORTEN
14
14
ZUGRIFFE
1143
1143
EMPFEHLEN
-
19.01.05 12:53 #1
- Registriert seit
- Jun 2004
- Beiträge
- 21
Hallo,
ich habe folgendes Problem:
Wir schreiben ein Programm, das über Plugins erweiterbar sein soll. Diese Plugin liegen in Form von *.class Dateien in dem Verzeichnis plugins. Eine Liste der verfügbaren Plugins in der Form String[] bekomme ich, indem ich das Verzeichniss auslese und alle namentlich korrekten Plugins filtere.
Nun will ich aus diesem String[] eben diese Plugins, die in dem Ordner liegen instanziieren und diese Objekte anschließend in einen Vector legen. Ich wollte das Array in einer Schleife abarbeiten, allerdings fehlt mir das Wissen, wie ich aus diesen String, die exakt den Dateinamen entsprechen, die jeweiligen Objecte erzeugen.
Ich hoffe ihr könnt mir dabei helfen und danke schonmal im Vorraus für die Hilfe.
MfG
MariusMeuser
-
19.01.05 13:27 #2
- Registriert seit
- Oct 2004
- Ort
- Hessen
- Beiträge
- 310
Hallo,
dafür gibt es die methode Class.ForName(String),
um eine Instanz von dieser klasse zu bekommen machst du dann:
Object newObject = Class.forName(<Klassennamen>).newInstance();
wie es mit dem casten aussieht, weiss ich jetzt nicht allzu genau.
Hiermit hast du jetzt eine Instanz erzeugt die aber nicht auf die Funktionen der klasse zurückgreifen kann, dazu musst du noch einen type-cast machen.
Such mal hier im Forum nach dem stichwort:
Reflection, das ist nämlich das was Du hier machen willst....
Gruss
Torsten."There's nothing we have to fear, except Fear itself....."
-
Oder auch so:
Code :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 39 40 41 42 43 44 45
class TestClassApp{ Object o; Class cl; public TestClassApp(String className){ try{ cl = ClassLoader.getSystemClassLoader().loadClass(className); } catch(ClassNotFoundException e){ System.out.println("Class not found :-("); e.printStackTrace(); } } public void instanceObject(Object[] param){ try{ o = cl.getConstructor().newInstance(param); } catch(Exception e){ System.out.println("could not instantiate object from class :-("); e.printStackTrace(); } } public void executeMethod(String methodName, Object[] param){ try{ o = cl.getMethod(methodName).invoke(o, param); } catch(Exception e){ System.out.println("could not execute method on object"); e.printStackTrace(); } } public static void main(String[] args){ TestClassApp test = new TestClassApp("TestClass"); test.instanceObject((Object[])null); test.executeMethod("foo", (Object[])null); } }
Wobei natürlich zu beachten ist, das der CLASSPATH auf dem entsprechenden Verzeichniss
wo deine *.class Klassen liegen gesetzt ist...
//edit siehe dazu auch:
http://java.sun.com/j2se/1.4.2/docs/...assLoader.html
Gruß
RedWingGeändert von RedWing (19.01.05 um 14:06 Uhr)
"I'm not deaf, I'm ignoring you"
----
-
20.01.05 14:44 #4
- Registriert seit
- Jun 2004
- Beiträge
- 21
Erstmal THX.
Ich bekomme hierbei leider 2 Fehlermeldungen beim compilieren des Codes. (siehe Code)
Wäre echt toll, wenn ihr das nochmal schnell korrigieren könntet. Ich gehe mal davon aus,
dass es sich nur um kleine Fehler handeln kann !
Code :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 39 40 41 42 43 44 45 46 47 48 49 50
class TestClassApp{ Object o; Class cl; public TestClassApp(String className){ try{ cl = ClassLoader.getSystemClassLoader().loadClass(className); } catch(ClassNotFoundException e){ System.out.println("Class not found :-("); e.printStackTrace(); } } public void instanceObject(Object[] param){ try{ o = cl.[COLOR=Red]getConstructor[/COLOR]().newInstance(param); Fehler: "createClassByName.java": getConstructor(java.lang.Class[]) in java.lang.Class cannot be applied to () } catch(Exception e){ System.out.println("could not instantiate object from class :-("); e.printStackTrace(); } } public void executeMethod(String methodName, Object[] param){ try{ o = cl.[COLOR=Red]getMethod[/COLOR](methodName).invoke(o, param); Fehler: "createClassByName.java": getMethod(java.lang.String,java.lang.Class[]) in java.lang.Class cannot be applied to (java.lang.String) } catch(Exception e){ System.out.println("could not execute method on object"); e.printStackTrace(); } } public static void main(String[] args){ TestClassApp test = new TestClassApp("TestClass"); test.instanceObject((Object[])null); test.executeMethod("foo", (Object[])null); } }
-
20.01.05 14:47 #5
- Registriert seit
- Oct 2004
- Ort
- Hessen
- Beiträge
- 310
Und wie lauten die genauen Meldungen der Fehler?
Könntest du das vielleicht noch posten, wäre hilfreich!
Danke
Gruss
Torsten
EDIT: Argh, okay, stehen ja im Source drin, demnächst der übersichtlichkeit einfach, nicht im Code reinhängen.
"There's nothing we have to fear, except Fear itself....."
-
20.01.05 14:57 #6
- Registriert seit
- Oct 2004
- Ort
- Hessen
- Beiträge
- 310
Hi erster fehler:
getClassConstructor() existiert so nicht, ein blick in die API Docs ergibt folgendes:
Constructor getConstructor(Class[] parameterTypes)
Returns a Constructor object that reflects the specified public constructor of the
class represented by this Class object.
Du versuchst eine Funktion aufzurufen, welche es gar nicht gibt.
zweitens:
selber fehler, die korrekte syntax ist:
Method getMethod(String name, Class[] parameterTypes)
Returns a Method object that reflects the specified public member method of the
class or interface represented by this Class object.
wobei das Array parameterTypes von klassen folgendes bedeutet: <zitat>:
The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes is null, it is treated as if it were an empty array.
<zitatende>
Sprich du kannst, wenn du keine Parameter übergeben brauchst nur null als param mitzugeben. Ansonsten, die Parameter in benötigter Reihenfolge.
Kurzer Tipp: Entweder eine geeignete IDE wie z.B.:Eclipse benutzen, welche hilft solche fehler vorzubeugen und/oder die API Docs von Java zu rate ziehen für eine korrekte anwendung der Funktionen
Gruss
Torsten"There's nothing we have to fear, except Fear itself....."
-
20.01.05 15:02 #7
- Registriert seit
- Jun 2004
- Beiträge
- 21
Der Code stammt nicht von mir, sondern wurde als Lösung auf die Frage dieses Threads
gepostet !
Daher meine Frage woher diese Fehler rühren. Das da was nicht stimmt wusste ich auch schon.
Ich dachte daher daran, dass man (der poster dieses Codes) vielleicht ne funktionsfähige
Version posten könnte.
MfG
Marius Meuser
-
20.01.05 15:15 #8
- Registriert seit
- Oct 2004
- Ort
- Hessen
- Beiträge
- 310
Axo,
naja, jetzt kannste ja den Code selbst korrigieren, kann schon mal im eifer des gefechtes passieren
Sollte ja auch kein Vorwurf sein, war nur eine kleine Empfehlung zum besseren Arbeiten
Gruss
Torsten."There's nothing we have to fear, except Fear itself....."
-
Zitat von MariusMeuser
Mhm bei mir hat das ganze perfekt compiliert.
Zitat von torsch2711
Allerdings arbeite ich auch unter 1.5 und hab den Code nich abwärtskompatibel
gebaut.
In 1.5 isses mittlerweile möglich einer Funktion beliebige Parameter zu übergeben
die dann als Feld innerhalb der Funktion angesprochen werden können.
Die Funktion Class.getConstrtor() der Java API is so eine.
Da das < 1.5 nicht möglich ist ließ sich der Code bei dir auch nicht kompilieren,
sorry nochmal dafür
Bsp:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
class TestClassApp{ static int add(int... a){ if(a.length == 2) return a[0] + a[1]; else if(a.length == 1) return a[0] + 2; else return 0; } public static void main(String[] args){ System.out.println(add()); System.out.println(add(1)); System.out.println(add(1, 2)); } }
Gruß
RedWing"I'm not deaf, I'm ignoring you"
----
-
23.01.05 16:29 #10
- Registriert seit
- Jun 2004
- Beiträge
- 21
Sop, habe jetzt den Code entsprechend angepasst.
Allerdings bekomme ich eine Fehlermeldung, die ich nicht zuordnen kann !
Fehlermeldung:
java.lang.ClassNotFoundException: TestPlugin
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at mconvert.CreateClassByName.<init>(CreateClassByName.java:11)
at mconvert.PluginManager.initiatePlugins(PluginManager.java:56)Class not found :-(
at mconvert.PluginManager.main(PluginManager.java:70)
java.lang.NullPointerException
at mconvert.CreateClassByName.instanceObject(CreateClassByName.java:22)
at mconvert.PluginManager.initiatePlugins(PluginManager.java:57)
at mconvert.PluginManager.main(PluginManager.java:70)
could not instantiate object from class :-(
Die entsprechenden Klassen:
Code :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
public Vector initiatePlugins() { this.loadPlugins(); Vector v = new Vector(); try { for (int i = 0; i < availablePlugins.length; i++) { CreateClassByName cl = new CreateClassByName(availablePlugins[i]); Object o = cl.instanceObject((Object[])null); v.add(o); } } catch (Exception e) { System.out.println("Fehler: " + e.getMessage()); } return v; } public static void main(String[] args) { PluginManager pm = new PluginManager(); pm.initiatePlugins(); /* for (int i = 0; i < pm.availablePlugins.length; i++) { System.out.println(pm.availablePlugins[i]); System.out.println(pm.pluginsPathes[i]); }*/ } }
Code :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
class CreateClassByName { Object o; Class cl; public CreateClassByName(String className) { try { cl = ClassLoader.getSystemClassLoader().loadClass(className); } catch (ClassNotFoundException e) { System.out.println("Class not found :-("); e.printStackTrace(); } } public Object instanceObject(Object[] param) { try { o = cl.getConstructor(null).newInstance(param); } catch (Exception e) { System.out.println("could not instantiate object from class :-("); e.printStackTrace(); } return o; } public void executeMethod(String methodName, Object[] param) { try { o = cl.getMethod(methodName, null).invoke(o, param); } catch (Exception e) { System.out.println("could not execute method on object"); e.printStackTrace(); } } }
Das hier ist die CLass die initialisiert werden soll:
Code :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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
package mconvert; /** * <p>Title: MConvert</p> * <p>Description: Multiformat Converter</p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author Die 2 Lustigen 3 * @version 1.0 */ public class TestPlugin implements PluginInterface { public TestPlugin(){ System.out.println("Instanz des TestPlugins erfolgreich erzeugt"); } /** * convert * * @param source String * @param target String * @param form Format * @todo Implement this mconvert.PluginInterface method */ public void convert(String source, String target, Format form) { System.out.println("Convert Mehtode erfolgreich aufgerufen !"); } /** * getName * * @return String * @todo Implement this mconvert.PluginInterface method */ public String getName() { return "TestPlugin"; } /** * hasStatus * * @return boolean * @todo Implement this mconvert.PluginInterface method */ public boolean hasStatus() { System.out.println("hasStatus erfolgreich aufgerufen"); return false; } /** * getStatus * * @return String * @todo Implement this mconvert.PluginInterface method */ public String getStatus() { return "hier kommt der Status"; } /** * hasOptions * * @return boolean * @todo Implement this mconvert.PluginInterface method */ public boolean hasOptions() { System.out.println("hasOptions erfolgreich aufgerufen"); return false; } /** * showOptions * * @param Format String[] * @todo Implement this mconvert.PluginInterface method */ public void showOptions(String[] Format) { System.out.println("showOptions erfolgreich aufgerufen"); } /** * getConvertableFormats * * @return FormatList * @todo Implement this mconvert.PluginInterface method */ public FormatList getConvertableFormats() { System.out.println("getConvertableFormats erfolgreich aufgerufen"); return null; } /** * stopAll * * @todo Implement this mconvert.PluginInterface method */ public void stopAll() { System.out.println("stopAll erfolgreich aufgerufen"); } /** * isBusy * * @return boolean * @todo Implement this mconvert.PluginInterface method */ public boolean isBusy() { System.out.println("isBusy erfolgreich aufgerufen"); return false; } }
Wäre tolle wenn jemandem auffällt, was ich falsch gemacht habe !
Habe bei mir das JDK 1.5 installiert und auch in meiner IDE eingetragen, scheint alledings nicht verwendet zu werden :(
MfG
Marius Meuser
-
Hallo,
Zitat von Marius
Hast du den auf das entsprechende Verzeichniss gesetzt?
Zitat von RedWing
Gruß
Redwing"I'm not deaf, I'm ignoring you"
----
-
23.01.05 17:00 #12
- Registriert seit
- Jun 2004
- Beiträge
- 21
Die Datei liegt in dem Arbeitsverzeichnis meiner IDE (JBuilder) und ich denke doch,
dass diese den CLassPath richtig setzt, wenn ich nen Testlauf mache.
Also JA.
-
23.01.05 17:08 #13
- Registriert seit
- Jun 2004
- Beiträge
- 21
So noch ne Erweiterung:
mit diesem Code:
Code :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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
import javax.swing.*; import java.awt.Toolkit; import java.awt.event.*; public class BeispielThread extends JFrame implements Runnable { private Thread thread = null; private JButton stopJButton = null; private boolean myBreak = false; public BeispielThread() { super(); System.out.println("Class: StoppUhr | void StoppUhr()"); getContentPane().setLayout(null); initComponents(); initGUI(); setVisible(true); } public static void main(String[] args) { System.out.println("Class: StoppUhr | void main(String[] args)"); new BeispielThread(); } public void run() { System.out.println("Class: StoppUhr | void run()"); while (!myBreak) { try { //mach was } catch (Exception e) {} ; } } public void initComponents() { System.out.println("Class: StoppUhr | void initComponents()"); setBounds(0, 0, 123, 92); setTitle("StopIt"); } private void initGUI() { System.out.println("Class: StoppUhr | void initGUI()"); stopJButton = new JButton(); stopJButton.setBounds(60, 37, 45, 20); stopJButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stopJButtonActionPerformed(); } }); getContentPane().add(stopJButton); } public void stopJButtonActionPerformed() { System.out.println("Class: StoppUhr | void stopJButtonActionPerformed()"); myBreak = true; } private void playJButtonActionPerformed() { System.out.println("Class: StoppUhr | void startJButtonActionPerformed()"); thread = new Thread(this); thread.start(); myBreak = false; } }
funktioniert es ! Sehe nur nicht warum es dann nicht auch bei meinem TestPlugin geht !
-
Sorry hab ich überlesen:
Deine Klasse Testplugin ist in nem package definiert namens: "mconvert"
Das musst du beim Laden der Klasse im Klassennamen natürlich mit angeben,
falls dus noch nicht getan hast:
Code :1
new CreateClassByName("mconvert.TestPlugin");
Gruß
RedWing"I'm not deaf, I'm ignoring you"
----
-
23.01.05 17:47 #15
- Registriert seit
- Jun 2004
- Beiträge
- 21
Alles klar, jetzt geht es, ein riesen THX !
Damit ist das Thema erledigt und der Thread kann geschlossen werden
---CLOSED---
Ähnliche Themen
-
Frage: kann ich mit einem String eine Klasse "definieren"?
Von Martin951995 im Forum JavaAntworten: 2Letzter Beitrag: 17.01.11, 21:53 -
Wie erzeuge ich den Effekt wie bei einem Hologramm
Von ElGreco93 im Forum Cinema 4DAntworten: 4Letzter Beitrag: 27.10.07, 12:50 -
Per JS einem li eine CSS Klasse vergeben
Von hanzen im Forum Javascript & AjaxAntworten: 9Letzter Beitrag: 31.10.06, 00:03 -
Von einem String zu einer Klasse konvertieren
Von fedora_core im Forum .NET ArchivAntworten: 1Letzter Beitrag: 19.02.04, 16:42 -
wie lösche ich auf einem ftp eine datei mit einem \".\" vor dem dateinamen ?
Von micha im Forum PHPAntworten: 2Letzter Beitrag: 19.01.03, 15:22





Zitieren
Login




