B4J Tutorial Multiple entry jar

You can put more than one java application in a single jar, but still be able to run them as individual applications.
The main advantage is the space it takes up.
For example, a simple non-ui program
B4X:
...
Sub AppStart (Args() As String)
Log("from class1")
End Sub
...
Takes about 91Kb when compiled to a jar.

If you had three similar programs, that's around 270Kb total on disk.

If you make a single program with 3 entry points, that does exactly the same as the three separate programs, it works out around 94Kb. Thus saving you ~180Kb of disk space and faster to download to/send to another computer.

Making the multi-entry point jar is easy
a, for each application - change the package name
ie, application#1 - package name = 'b4j.example.class1'
application#2 - package name = 'b4j.example.class2'
application#3 - package name = 'b4j.example.class3'
b, compile in release application#2 and #3
c, in main application #1, add the #AdditionalJars: for application #2 & #3
d, compile in release application #1

to run them from a batch file requires a minor change (assuming .bat file is in the same directory as the jar files)
B4X:
java -cp ./class1.jar b4j.example.class1.main
java -cp ./class1.jar b4j.example.class2.main
java -cp ./class1.jar b4j.example.class3.main
They will run as if separate applications, but they exist in only one jar file.

The downside is - if you change 1 application, you have 2 to compile again (the one that changed and the main application to include the new changed jar)
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
How to run application #2,#3 in the application #1 ? :oops:
 

Daestrum

Expert
Licensed User
Longtime User
In the code above

Line 1 runs application#1 (java -cp ./class1.jar b4j.example.class1.main)
Line 2 runs application#2 (java -cp ./class1.jar b4j.example.class2.main)
Line 3 runs application#3 (java -cp ./class1.jar b4j.example.class3.main)
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi
merry chrismas. your mean is call they with jshell in the application#1.
 

Daestrum

Expert
Licensed User
Longtime User
your mean is call they with jshell in the application#1
No, from the command line (or a batch file).
class1.jar contains ALL of the applications ( #1, #2 & #3) you are simply calling the entry point for which application you wish to run.
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Could i pass Args() to application#~#3 ?
 

Daestrum

Expert
Licensed User
Longtime User
I would guess something like
passing the arguments : 1 2 3 4
B4X:
java -cp ./class1.jar b4j.example.class3.main 1 2 3 4
 

Magma

Expert
Licensed User
Longtime User

le_toubib

Active Member
Licensed User
Longtime User
hiii
in b4j , how do i run the 2nd jar file from the first one ??
normally i would use :
B4X:
Dim shl As Shell
shl.Initialize("shl","javaw.exe", Array As String("-jar", "jarfile.jar"))
            shl.WorkingDirectory=File.DirApp
            shl.Run(-1)
 

Daestrum

Expert
Licensed User
Longtime User
Possibly something like (not tried it)
B4X:
Dim shl As Shell
shl.Initialize("shl","javaw.exe", Array As String("-jar", "-cp","jarfile.jar","b4j.example.class2.main"))
            shl.WorkingDirectory=File.DirApp
            shl.Run(-1)
 
Top