England
Joined: May 16, 2014
Post Count: 33
Status:
Offline
How to load "foreign" jars from a Plugin?
Folks
I've written a Plugin that uses 3 other jars.
-rw-r--r--@ 1 rmills staff 23K 21 Jan 13:52 json-simple-1.1.1.jar -rw-r--r--@ 1 rmills staff 23K 15 Jan 11:44 simple.jar -rw-r--r--+ 1 rmills staff 312K 28 May 2014 ST-4.0.8.jar
When I execute SH3D on Eclipse, everything is working OK (Mac/Windows/Linux).
However, I'm wondering how to deploy this on a user's computer. On the Mac, I've added <string>path-to-jar</string> to <SH3D>.app/Contents/Resources/Info.plist in <key>ClassPath</key><array>...</array> On Linux, I've modified the -classpath argument in the script SweetHome3D. I've given up on Windows, although it's working from Eclipse. Of course, I don't expect users to modify those files. I've done that to debug my code.
The question is: Is there a directory into which jars should be added for use by plugins?
I've looked at the APIs in Java and I believe URLClassLoader will solve this issue. Before I hurt my head getting that to work, I wondered if there's something already in SW3D to handle this.
France
Joined: Nov 7, 2005
Post Count: 9423
Status:
Offline
Re: How to load "foreign" jars from a Plugin?
Did you try to put all the classes of the jars you need in the plug-in file it self?
----------------------------------------
Emmanuel Puybaret, Sweet Home 3D creator
England
Joined: May 16, 2014
Post Count: 33
Status:
Offline
Re: How to load "foreign" jars from a Plugin?
Thanks, Emmanuel. I can't persuade Eclipse to do that.
I've tried unzipping the jar and adding it that way. Then I get compilation errors. However, that can't work because the tree of dependencies could be vast. And we don't want duplicate code loaded. I'm not sure jars should be in a jar. If another Plugin happened to use the same jar, you'd want the JVM to share. So, I think we have to tell the JVM - "load it from here".
The Plugin loads and executes (updates the UI). The moment the Plugin wants to use anything from the jars, it throws ClassDefNotFound. It feels to me that I have to modify the CLASSPATH and then he'd find the libraries. However, I think CLASSPATH is only used when java starts to initialise the ClassLoader.
I've tried other ideas such as putting the jars in the plugins directory and into the SW3D directories.
Tomorrow's another day. This isn't difficult, however it is mysterious! I'm going to the dentist tomorrow. That'll be more fun.
France
Joined: Nov 7, 2005
Post Count: 9423
Status:
Offline
Re: How to load "foreign" jars from a Plugin?
I would do it first "by hand" with a zip utility, i.e. once you compiled the classes of your plug-in and generated a jar file from them with Eclipse, open the jar file with a zip utility and add to it the content of json-simple-1.1.1.jar, simple.jar and ST-4.0.8.jar. I've already done that in the past for some plug-ins, to include SunFlow classes or even JMF library. Once it works, I think it should be time to learn Ant basics to automate compilation and plug-in generation. Ant is some kind of make but much simpler and nevertheless powerful. All Sweet Home 3D files I release are generated with Ant that you can even launch from Eclipse. With 1189 lines, the Ant build.xml file of Sweet Home 3D is probably not the easiest way to understand how Ant works, but compiling, creating a jar file and merging jar files is something you can do in just a few lines.
we don't want duplicate code loaded. I'm not sure jars should be in a jar.
You should include classes only in the final jar. Including jars would require a special class loader to extract them and it's a needless tedious task.
If another Plugin happened to use the same jar, you'd want the JVM to share.
The jar files you want to include are small and you shouldn't worry about that.
Hope you can make it...
----------------------------------------
Emmanuel Puybaret, Sweet Home 3D creator
England
Joined: May 16, 2014
Post Count: 33
Status:
Offline
Re: How to load "foreign" jars from a Plugin?
Emmanuel
You are 100% correct. A big round of applause for M Puybaret.
When I merge the jars, everything works. I've added a script 'build' to the plugins directory. I'll figure the Ant/Eclipse/export later.
885 rmills@rmillsmbp:~/Library/Application Support/eTeks/Sweet Home 3D/plugins $ dir -rw-r--r--+ 1 rmills staff 131K 3 Mar 13:49 ToWebPlugin.jar -rwxr-xr-x@ 1 rmills staff 348B 3 Mar 13:48 build* -rw-r--r--+ 1 rmills staff 312K 2 Mar 13:28 ST-4.0.8.jar -rw-r--r--@ 1 rmills staff 23K 2 Mar 13:28 json-simple-1.1.1.jar 886 rmills@rmillsmbp:~/Library/Application Support/eTeks/Sweet Home 3D/plugins $ ./build -rw-r--r--+ 1 rmills staff 133765 3 Mar 13:49 ToWebPlugin.jar -rw-r--r--+ 1 rmills staff 399906 3 Mar 13:50 ToWebPlugin.jar 887 rmills@rmillsmbp:~/Library/Application Support/eTeks/Sweet Home 3D/plugins $
#!/bin/bash
## # build ToWebPlugin.jar ##
# static data foo=foo jar=ToWebPlugin.jar
if [ -e "$jar" ]; then ls -alt "$jar" rm -rf "$foo" mkdir "$foo" for i in *.jar ; do ( cd "$foo" jar xf ../$i ) done cd $foo jar cf "../$jar" * cd .. rm -rf "$foo" ls -alt "$jar" else echo "file $jar does not exist" fi
# That's all Folks! ##
Thank You very much indeed for your help. Merci Beaucoup!