Bibliotheken Nachladen

K

keymail

Guten Tag,

ich würde gerne ein jar-file zur laufzeit laden damit es dem
Classloader bekannt ist. Sozusagen wie als wenn es im Classpath beim
Start vorhanden wäre.

Ich habe ein Plugin für ein Plugin (ist nötig), welches
folgendermaßen aussieht:

Kernprogramm (wird gestartet)
--------------------------------------------------
Ebene 1: Plugin für das Kernprogramm (wird vom Kernprogramm geladen)
--------------------------------------------------
Ebene 2: Plugin für das Plugin (wird/soll von Ebene 1
gestartet/werden)

das dumme ist das das es nötig ist das Ebene 1 die abstracte Klasse
aus einem jar kennen
muss um zu merken das Plugin der Ebene 2 diese abstracte klasse
extended.
Jedes Plugin der Ebene 2 muss die abstract class "Plugin" extenden
damit es auf Ebene 1 als Plugin erkannt wird.

Leider kann Ebene 1 nur erkennen wie die Klasse auf Ebene 2 heisst aber
nicht das sie die abstrakte Klasse "Plugin" erweitert. Das führt
einfach dazu das die Abfrage if (o instance of "Plugin") nicht hinhaut
und somit ein Fehler erzeugt wird, obwohl o die abstracte Klasse
"Plugin" erweitert.

Geladen wird die Klasse folgendermaßen:

JarClassLoader jarLoader = new
JarClassLoader(this.pluginFile.getAbsolutePath());
String className = jarLoader.getClassNamefromManifest();
Class c = jarLoader.loadClass(className, true);
Object o = c..newInstance();

if(o instanceof Plugin) {
this.plugin = (Plugin) o;
}

Das ganze funktioniert einwandfrei wenn das Kernprogramm das jar-file
mit der Klasse "Plugin" im Classpath enthält. Das ist aber für ein
Plugin normal nicht möglich und ich will das nicht. Gibt es also eine
Möglichkeit wie man dieses Jar vorher dynamisch dem Classpath
hinzufügen kann?
 
S

Simon

Note that this is an English speaking news group.

I will try to translate your question, though it is not easy to understand what
the actual problem is.

- You have a plugin (in a jar) loaded from the main program.
- You have a second plugin (in a second jar) loaded from within the first plugin.
- You cannot cast this second plugin to your abstract superclass "Plugin"
because of problems with the class loader which I do not quite understand.

As a workaround you want to add the plugin jar to the classpath at runtime. As
far as I can tell, this workaround is impossible. However, your way of loading
the plugins through a ClassLoader should actually work fine.

One problem I can think of is the way you compile and bundle the plugin jars.
Make sure that there is no second or third version of the Plugin superclass
contained in the jar file. If that does not help, please provide some more
information:

- error messages and stack traces (if you don't get an Exception because your
test "o instanceof Plugin" fails, it might be good to remove the if-statement
temporarily and just let the typecast throw an Exception just to get some more
information)
- a list of (relevant) classes contained in the respective jar files and on the
class path
- maybe post a SSCCE

Cheers,
Simon
 
K

keymail

Oh, this is my first Post and imediatally in the wrong Forum ^^ sorry.

I tried to remove the if-clause and the typecast throws the following.

If i had a Class MyPlugin extends Plugin for example a
ClassCastException is throwed

"Plugin" is required and "MyPlugin" is found

but MyPlugin extends Plugin. That is the Problem. The Programm doesn´t
recognize that MyPlugin extends Plugin and the statement if(o
instanceof Plugin) returns false.

If i put the jar-file with the Plugin.class in the classpath of the
Mainprogramm everything is fine, but that is normaly not possible for
an plugin-coder.
 
S

Simon

Oh, this is my first Post and imediatally in the wrong Forum ^^ sorry.

Never mind!
I tried to remove the if-clause and the typecast throws the following.

If i had a Class MyPlugin extends Plugin for example a
ClassCastException is throwed

"Plugin" is required and "MyPlugin" is found

but MyPlugin extends Plugin.

That is what your exception says? My exceptions look like this:

Exception in thread "main" java.lang.ClassCastException: my.package.MyPlugin
at Test.main(Test.java:6)

What Java version are you using? Your message looks almost like a compiler error...
If i put the jar-file with the Plugin.class in the classpath of the
Mainprogramm everything is fine, but that is normaly not possible for
an plugin-coder.

In my previous post I said I believe it could be a problem if a second or third
version of the superclass "Plugin" exists in the jars. Now, is Plugin.class in
your jar file? In that case I'm not surprised that the JVM gets confused.

I don't think anyone can answer your question unless you provide more
information. Post a SSCCE and a list of files in your jar files.

Cheers,
Simon
 
K

keymail

Hm ok, if i understood your hint with the second or third version of
the class you mean that if i include the Plugin.class in all three jars
it cause the problem.

I testet all variations.
I call the jar with the Plugin.class plugin.jar.

1. include plugin.jar in the mainprogramm -> everything is ok and it
does not matter if the plugin.jar is also in the firstlevelplugin
and/or secondlevel plugin.

2. include only in fistlevel jar -> ClassCastException is thrown.

3. include only in secondlevel.jar -> java.lang.NoClassDefFoundError:
com/api/Plugin

I hope you understand that i could not post the complete code or
exception because maybe i will get some problems with my company.

I use jdk 1.4.2_10 and there i have no choice :) this is a restriction
of the Mainprogramm i programm a plugin for.
 
S

Simon

Hm ok, if i understood your hint with the second or third version of
the class you mean that if i include the Plugin.class in all three jars
it cause the problem.

At least it *may* cause a problem.
I testet all variations.
I call the jar with the Plugin.class plugin.jar.

I assume that "Plugin" is the abstract superclass or interface that is
implemented by all plugins, right? And I assume (though I am not sure here) that
plugin.jar is supposed to contain an implementation of a plugin. Then I don't
understand why you would want to put Plugin.class into plugin.jar.
1. include plugin.jar in the mainprogramm -> everything is ok and it
does not matter if the plugin.jar is also in the firstlevelplugin
and/or secondlevel plugin.

2. include only in fistlevel jar -> ClassCastException is thrown.

3. include only in secondlevel.jar -> java.lang.NoClassDefFoundError:
com/api/Plugin

I don't understand a word. A file ("plugin.jar") can be contained in a directory
or in the classpath or whatsoever. I don't understand what it means that a file
is included in a programme and I don't understand what it means that a file is
included in a plugin.

Why don't you just give a list of classes, files, etc. you have in your project
so that one can eventually make a guess about what could go wrong? The
information you post is just to vague.
I hope you understand that i could not post the complete code or
exception because maybe i will get some problems with my company.

If you cannot even post a stack trace or a SSCCE then your company should maybe
hire a consultant.
I use jdk 1.4.2_10 and there i have no choice :) this is a restriction
of the Mainprogramm i programm a plugin for.

That should not be a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top