Load jar file and dependencies at runtime

3,990 views
Skip to first unread message

paolo...@gmail.com

unread,
Feb 2, 2016, 10:06:25 AM2/2/16
to ComfyJ Forum
Dear Support,

one of our customers is asking us to load dependency jars at run-time.
They just want to include our jar in the class-path and then our jar loads all the dependencies at runtime.
Our code use Comfyj to include an activex into a java component.

In my code I can use URLCLassLoader to load OleContainer instance, but what happen to all the dependencies behind OleContainer class?

Do you have any solution for this problem?
Is this a feasible approach?

Thanks
Paolo.

Sergei Piletsky

unread,
Feb 2, 2016, 11:06:30 AM2/2/16
to paolo mosna, ComfyJ Forum
Hi Paolo,

Technically we do not have any solution for this problem, because it has nothing to do with ComfyJ at all. You can find the same question on the following StackOverflow thread: http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime

Anyway, it does not seem to be a feasible approach. The simple reason is that OleContainer uses a lot of classes from ComfyJ, JNIWrapper and WinPack libraries, so you will need to load all of them, including all subsequent dependencies.

Much better way, if JAR size in an issue, would be to shrink all used Jar files using some obfuscator utility, for example ProGuard: http://proguard.sourceforge.net/ This application allows to remove all unused classes from JAR libraries and this may significantly reduce their size.

Please let me know if you have any further questions.

Regards,
Sergei


--
You received this message because you are subscribed to the Google Groups "ComfyJ Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to comfyj-forum...@teamdev.com.
To view this discussion on the web visit https://groups.google.com/a/teamdev.com/d/msgid/comfyj-forum/83f1b4c6-924a-4f3f-9393-daaa5b115d2a%40teamdev.com.

Paolo Mosna

unread,
Feb 2, 2016, 12:56:08 PM2/2/16
to Sergei Piletsky, ComfyJ Forum
The only way was to do this:
 

URLClassLoader

classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

Class

clazz= URLClassLoader.class;

// Use reflection

Method

method= clazz.getDeclaredMethod("addURL", new Class[] { URL.class });

method.setAccessible(true);

String instPath = WinRegistry.readString (

WinRegistry.HKEY_LOCAL_MACHINE, //HKEY

"SOFTWARE\\Reconice\\Rvrc", //Key

"InstPath"); //ValueName

System.

out.println("Recomed Inst Path [" + instPath + "]");

String

jarPath = instPath+File.separator+"java"+File.separator+"jar"+File.separator;

ArrayList<String>

jarFileList = new ArrayList<String>();

jarFileList.add(jarPath+"comfyj-2.11.jar");

jarFileList.add(jarPath+"jniwrap-3.9.jar");

jarFileList.add(jarPath+"license.jar");

jarFileList.add(jarPath+"log4j-api-2.1.jar");

jarFileList.add(jarPath+"log4j-core-2.1.jar");

jarFileList.add(jarPath+"winpack-3.9.jar");

jarFileList.add(jarPath+"slf4j-api-1.5.8.jar");

jarFileList.add(jarPath+"slf4j-simple-1.5.8.jar");

for(String jar : jarFileList){

File

f = new File(jar);

if(f.exists() == false){

throw new Exception("File [" + jar + "] doesn't exist!");

}

System.

out.println("Adding jar [" + jar + "]");

method.invoke(classLoader, new Object[] { f.toURL() });

}

 
but out of standard I think.
 
Paolo.
Hi Paolo,
Regards,
Sergei


Reply all
Reply to author
Forward
0 new messages