B4J Tutorial SOLUTION??? Merging jars

After searching the forums and asking around there seems to be no common, simple solution to this.

SCENARIO

You have created some java mywrapper.java that wraps an existing jar wrapped.jar

You do not have the source of wrapped.jar

You set up a source folder structure like:

D:\mywrapper
|
+--- src\mywrapper.java
|
+--- libs\wrapped.jar

You compile mywrapper.java using B4J_LibraryCompiler.exe and end up with mywrapper.jar and mywrapper.xml in your B4J ....\Additional Libraries folder

But mywrapper.jar does not have wrapped.jar embedded in it, if you want to use the new B4J library mywrapper in a B4J project you have to include this in the project:

#Region Project Attributes
#AdditionalJar: wrapped.jar
#End Region

and there has to be a copy of wrapped.jar in the B4J ....\Additional Libraries folder

All very messy.

SOLUTION

I had this scenario here and was able to find a solution courtesy of Claude which I present in a more generalised form here:

Create a folder structure, say:

D:\mywrapper_merge
|
+--- mywrapper.jar (from B4J ...\Additional Libraries)
|
+--- wrapped.jar
|
+--- merge.bat

Where merge,bat contains:

B4X:
@echo on
:: Post-build fat JAR builder
:: Place this .bat next to both JARs

set OUTPUT=mywrapper.jar
set DEP=wrapped.jar
set WORK=_fatjar_tmp

:: Extract dependency
mkdir %WORK%
cd %WORK%
jar xf ..\%DEP%
cd ..

:: Extract your library into same temp dir (dependency goes first so your classes win)
cd %WORK%
jar xf ..\%OUTPUT%
cd ..

:: Remove META-INF signatures from wrapped (causes SecurityException if left in)
del /q %WORK%\META-INF\*.SF 2>nul
del /q %WORK%\META-INF\*.RSA 2>nul
del /q %WORK%\META-INF\*.DSA 2>nul

:: Repack everything into the output JAR
cd %WORK%
jar cf ..\%OUTPUT% .
cd ..

:: Clean up
rmdir /s /q %WORK%

echo Done. %OUTPUT% now contains wrapped classes.
pause

after running merge.bat you should now have a updated D:\mywrapper_merge\mywrapper.jar with wrapped.jar embedded in it.

Copy D:\mywrapper_merge\mywrapper.jar ...\Additional Libraries\mywrapper.jar

Now you can use the mywrapper library without having to use:

#Region Project Attributes
#AdditionalJar: wrapped.jar
#End Region

and no need for a copy of wrapped.jar in the B4J ....\Additional Libraries folder
 
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
So similar to what I posted already?
Agreed - not sure if the

del /Q .\all\*.class
del /Q .\all\*.html
del /Q .\all\LICENSE
del /Q .\all\NOTICE

should be treated as a general requirement - thoughts?
 

tchart

Well-Known Member
Licensed User
Longtime User
Agreed - not sure if the

del /Q .\all\*.class
del /Q .\all\*.html
del /Q .\all\LICENSE
del /Q .\all\NOTICE

should be treated as a general requirement - thoughts?
I think I came across those issues when b4j compiled my project in release mode.

Release mode creates another fat (merged) jar and for some reason it caused a conflict when those were in my combined lib.

Same reason your code is deleting the meta files.

Time will tell I guess.
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Time will tell I guess.
del /q %WORK%\META-INF\*.SF 2>nul
del /q %WORK%\META-INF\*.RSA 2>nul
del /q %WORK%\META-INF\*.DSA 2>nul

was generated by Claude - but the wrapped.jar (jsch-0.1.55.jar) I was merging into my mywrapper.jar seems to be very clean in regards to META-INF (i.e. nothing)

This implies Claude did not know the mywrapper.jar META-INF had nothing.

Which in turn implies Claude reckons these 3 del /q... are universally required.

If someone had a wrapped.jar that had other stuff in its META-INF there might be some fun and games.
 
Last edited:
Top