I sometimes run into JAR loading conflict issues in ColdFusion where two JARs may have the same class path but are using different versions. Lucee has already worked around this issue by having the ability to create a Java object reference using a bundle name and a version, and Adobe is reportedly also working on this for their 2020 release. Having run into this issue too many times, I decided to create a CommandBox script which creates bundled and versioned JARs so I can at least get around these issues now on Lucee.
OSGi to the Rescue
With Eknath Pawar‘s help I was able to narrow down the steps needed to create a bundled JAR version:
- cd into the folder of the JAR.
- Expand the contents of the JAR file. Don’t forget a JAR is basically a ZIP archive.
- Delete the meta-inf folder under the expanded content, if there is one.
- Add a manifest.txt file with the bundle properties that I need under the root.
- cd into the expanded JAR folder.
- Run the jar cvfm command to create a new JAR.
The last step uses the jar command which is the Java executable usually found under the JDK bin folder at C:\Program Files\Java\jdk1.8.0_231\bin\jar.exe. So, make sure you have the Java JDK installed on your computer and two Path variables defined in your Windows Environment for this module to work. For example, on my PC these Windows Environment variables are:
- JAVA_HOME = C:\Program Files\Java\jdk1.8.0_231
- Path += %JAVA_HOME%\bin
The finished code is packaged as a ForgeBox command under https://www.forgebox.io/view/commandbox-osgi.
Here is an example of how I used this ForgeBox command to create an OSGi bundle of a specific version of the Google Gson package, which can be used for JSON (de)serialization.
Similarly, I can use the module to loop through entire folders and create versions out of all the JARs it finds. It can even do a recurse lookup for nested folders.
No more JAR class instantiation woes. Take this command out for a spin and let me know how it goes.