How to use jlink, jpackage and docker to put your Java on a diet! (Custom JDK/JRE)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome back to devexplaining channel today i wanted to talk a little bit about the built-in tools that you get with jdk these days so i will be discussing j-link and j-package and a little bit of docker to add in other words we are going to put java on a diet so let's get started i'm showing you some things based on my earlier video series test data maker this is a little little spring boot application you don't need to know much about it to get this video but if you if you're curious i have like about three other videos showing some phases on how i got started with spring boot but uh only thing i need right now is is basically that i have a jar file that i can run and second thing is that i will be including all the codes that i'm showing here in my project and i will be putting this link in the description section so if you like you can grab it from there and study at your own time let's open the test data maker repository and i will show you some things i'm running now java 16. i think some of these tools started coming along already after java 9 when the modules were introduced so i don't remember clearly when they were around but the these have been around for quite some time java 17 java 18 will all include these tools and the first tool i wanted to show was j-link if i say just j-link you can see that it's included in my path because it's part of java 16. same thing as java c and java tools so so similar to you can say j-link help and you will be helped you can see what it it's able to do but there's a lot to digest so i'm just going to show you something simple i provided some scripts and as i said these are included in the project so you can clone it and study it but first thing i wanted to introduce was create minimal jr jra i have a bash script that's going to run j-link and it's going to add modules not all java modules because java's been around since 1995 and until java 9 and module module system people just kept on adding more and more lib libraries and it gets bloated more and more bloated and we ended up having we ended up having things like midi instruments one megabyte of midi instruments included in in jd case and we got awt and swing and javafx graphical user interface libraries which you might not need if you are only doing services and and servers and then we got some other libraries that actually you might never end up using so it got more and more bloated until we got the modularity when we got the modularity features now you are able to pick up modules that you need and some things are straight being removed from the core how did i find these modules well i was running my application and i started with one module java base and then i just figure out what kind of exceptions i'm getting compared to when i run it with all the modules so what's the first missing thing what's the module that contains it and then i based on that i just added some simple modules and this is your typical list for any spring boot application but if you do anything fancier you might need more modules in my case that's the list that i need right now and at the end here i'm just saying that please put this output in a diet jra folder okay i actually have that folder let's remove it and run this little script create minimal this is now grabbing those modules from my default jdk installation i can also point out to other in installation even if it's not my kind of it can be cross-platform so i can point this to windows libraries instead but in this case i was pointing to default which is my ubuntu java libraries and if i take a look in diet jra folder then you can see that we get the bin folder for the binaries we got configurations include some legal stuff some manuals and most importantly the lib folder that now contains the modules that i wish to include this is my kind of minimal jdk just the bits that i absolutely need to run my little spring boot application and i provided here two scripts so i have a run straight this is running my application directly with the kind of default java that you have and then i have run with diet jra and this one is all only running the diet version that i just created so it's a little bit uh kind of different setup so that's part one j-link and what you can do with it is this really important well i don't know but i'm just dropping some stuff that i don't need so it means a little bit less memory a little bit less files to load which might be a little bit faster a lot less attack surface for the vulnerability so all the modules that i didn't include if there is some kind of new vulnerabilities discovered if they are in the areas i didn't include i'm immune to them unlike people using the full java virtual machine so to be honest might not be worth the hassle always but this is more for kind of intellectual curiosity and understanding what these tools do sometimes it's fun to kind of fine-tune things like this sometimes it's not really necessary you get to decide what what do you think by the way i love the comments in the feedback section so if you have an opinion whether to do this hassle or not whether this is interesting or just kind of trivial and not important let me know in the comment section i would love a little bit of feedback also if i make some mistakes let me know because then i will improve as well okay let's move on to the j package part so what's the package you're good for well packager will allow you to grab your own application and you can grab any j array so you can grab the default jdk include that and your code and put them all together in an installer or alternatively you can grab a custom custom kind of java that i just created you can grab that and your code and put them together this works whether you have a service or whether you have a kind of graphical user interface application i'm not going to elaborate on this too much because i don't typically myself use this so much but it's fast to show so while we are talking about jlink let's cover this one as well the scripts are included in the project script this package package sh will run j package tool similar than j linker i give it a name i tell where you can you can find the jar files input target then i tell what's the main jar so run this one and finally which runtime image to use if you don't say anything here you will just get the default java that includes the package tool but i'm including my diet version of java so i i run the package and what i get is platform specific installer binary that includes the java that you provided and it also includes your own code and any configuration so it's going to crunch there for a bit and what i end up with is test data maker 1 amd 64 deb so this is my kind of binary that i could then go on and install i find this for my purposes these are not necessary typically because i can always very easily just run to this little bash script and just say that java minus jar so typically i really don't need these binaries and you have to do them separately for each platform but if i really wanted to make very easy installation for kind of client-side applications then this would be much better than using some third-party tools probably okay but um i mainly showed this because it was very fast to show if you find some use for this if you're a big fan of this again let me know in the comment section as you can see it ended up getting quite fat as well so it's not a very lightweight or thin binary because we are still including a lot of a lot of jdk with it and a lot of libraries with it so it's quite heavy okay so final little trick here to show and that has to do with docker so i mentioned that i don't typically deal with these platform-specific installers because you can nowadays in many cases when you are doing services you can just put your code and java and operating system and put them in a container and ship the whole container that's the point of docker we could do that easily but the trick that i wanted to show you and the trick that's included in the project right now is a two-phase docker image so step one i'm having open jdk 17 image and running that as packager that's my first stage so only thing i'm doing is grabbing that open jdk jailing tool and giving it some modules to crunch and telling where to put the output so this is going to do the same thing that i just showed but within docker and i end up having stuff in this opt jdk 17 minimal folder so then kicks in my second stage which picks up some bass image i don't actually need open jdk anymore i would just need a similar bass image without any java but in this case i'm being very lazy i i could actually use some more lightweight linux versions as well that might make sense but as i said being lazy here still the principle remains the same whatever is your favorite linux kind of base package that you want to use or base image but then what i'm doing here is copying from packager that jdk 17 minimal in my new image then i'm copying my binaries there and now having all that then i'm setting up some environments to kind of override the default java with the java that i actually want to use this is just convenience i could obviously just use the full parts to be sure exposing port and running this so all together i'm also providing some script so we have build docker here let's do the build i have done it earlier so it's faster than typical nothing has changed but then i have run docker so i can run my whole docker container so then i'm running the kind of open jdk image it's now enhanced with my custom virtual machine that's on a diet just a minimal needed to run spring boot and then there's my jar file that contains spring boot binaries it's a fat jar all together within the container and i think that's kind of fun way to do things it makes things so easy that tailoring a little bit and tinkering with the virtual machines is not that difficult anymore so i wanted to share this with you did you find it interesting useful entertaining even click those buttons let me know in the comments section as for this video i think we are done thanks for watching and see you in the next one bye bye
Info
Channel: DevXplaining
Views: 348
Rating: undefined out of 5
Keywords:
Id: SRHqm9XjGOs
Channel Id: undefined
Length: 12min 12sec (732 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.