Java Beginner Tutorial 7 - JAR (Java Archive) basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this Java beginner tutorial today we are going to learn about jar we will see what a jar file is why do we need jar files how can we create a jar file from our project and then we will also look at how can we extract the contents out from jar file so let us begin and let us see what a jar file is so jar is an acronym for Java archive and what does this actually means is a jar file is a compressed format of compiled Java project to understand it let us look into this image so you can see when we compile our java code all the source files which are dot Java extension are compiled into bytecode which are files with dot classics extension and when we create a jar file all these class files or the bytecode is compressed and a jar file is created with all these class files and this jar file can be used by external programs and it can also be used to distribute our source code or our application to different people ok so if I have to write it down I will say this is a compressed format of a compiled Java project or Java program please notice this is a for compressed format of the compiled Java project so it will contain dot class files and not the dot Java files so we can say it contains dot class files and plus metadata and some other resources so or we will look into details what exactly we mean by this so it will be a combination of dot class files and then we can have some metadata which will be some information about these thoughtless files and in larger projects we can have some other resources like some text files or some image files as well okay and now why do we exactly need the jar files so it'll test needed and jar files are used to distribute Java code or you can say the Java libraries for use by other projects okay to understand this let us say let us go to a java mail jar if you go to your browser and type Java major mail jar you get an option to download this Java mail jar okay and if you go here you see all the dot class files are there along with some other files which are packaged into a zip file which we call as a jar file okay so what does this mean is somebody has created a project and has created all the source code for sending email through Java and then he has compiled his program and converted all the compiled class files into a jar file which is this jar file so when we get this jar file we get all the source code and we can use all these features into our Java program so we do not have to write a program to send email through Java right from scratch we can use the char file similarly if you are writing your program and you think that it can be used by somebody so you can compile your program package it into a jar file and then you can distribute it the other use is you can also use jar file for direct execution of Java program and by direct execution we mean without the use of any IDE so if you are able to compress and convert your program into a jar file you can use this for direct execution and you do not have to go to the Eclipse IDE or any other okay so this is what a jar file is in very basic manner and why do we need jar files now let us go to how to create jar file from Java project so let us say this is the Java project that we created this is a program that we created in our last sessions and if I want to create a jar file out of this what I am going to do is I will go to my project here and I will do right click and I will say export okay export and here I can type jar I get an option of jar file select this go to next and here you have to select the location okay so let us do one thing let us create a new folder where I will save all my jar files here I will create a new folder jar and I will give this location okay even if you are on Windows you will do the same thing you will create a folder or wherever you want you can save your jar file so I'm going to give this location this is jar and I will give HelloWorld dot jar and click on next click on next so here you will find it will it is asking for which is the main class okay this is important because you have to tell your jar file what is the main class that it has to execute so you I will select HelloWorld okay and finish warnings are okay so now if I go to this folder I have this HelloWorld dot jar okay now if I have to execute it I will go to my command prompt or the terminal in case of Mac so let me go to command prompt and let me go to this particular location I'm going to terminal because I am on Mac and I will go to this location and I will say Java space - jar and the name of my jar file and I will execute and it has executed my program okay so this is a becomes very handy that you can create a jar file out of your project and then you can execute it directly and you can also send this jar file to anybody and he can import this jar file into his project and all the code that you have written becomes available to the program and we will look into detail how exactly we do that okay now we have seen and we have said that jar is a compressed format of a compiled Java project and if then that is true we should also be able to decompress it so let us try let us right click on this jar file and you can use any unzip utility so in my case I am using this one so I'm unzipping it and I have got a folder I've got a folder with HelloWorld let me open this and what I have got is I have got my package and my dot class file and also I have got meta INF which is information about which is the main class so if I open it with some text editor let me open it with some text editor text editor and you can see this particular manifest dot M F file has the information about your main class so this is important and this becomes available then we select what is the main class that this jar file should execute so in case you do not use this you might get errors when you try to execute your jar file okay but if we go to this folder we have the byte code which is the dot class file sometimes you might also require to get back the Java file or the source code in that case what you can do is you can go to your browser and search for java decompiler okay when we compile our Java dot Java files it gets converted to dot class file and to convert dot class file back to dot Java file we have to use a D compiler and here you can use this particular D compiler and here this is a java decompiler this is a program written by someone and this is a very good utility to decompile the dot class files you can either download the GUI so you can select your operating system and download the GUI in my case I have done this for Mac or you can also add it as a plug-in to eclipse okay in my case I have downloaded the GUI so once you download and start it you will get this app and let me start it and here you will open the jar file so in my case it is located in desktop tools Java jar and HelloWorld dot jar and now you can see it has D compiled and given me back my dot Java file so I can look into my program exactly what this jar file contains ok so this is all about jar file some of the people say that jar is a topic which should be taught as an intermediate or advanced level of Java training but I believe that if you are clear with the very basic concepts of a topic it becomes very easy for you to understand the advanced concepts of the same topic so I believe this was very easy to understand and you got it in our next sessions we will look into more details about the jar file hope you like it keep learning Java thank you
Info
Channel: Automation Step by Step
Views: 54,951
Rating: undefined out of 5
Keywords: jar, java archive, what is jar, why jar, how to create jar, how to create jar file, java compiler, java decompiler, extract jar, java extract jar, Why do we need JAR files, How to create JAR file from Java Project, How to extract contents from a JAR file, jar basics, java basics, learn java, learn automation, jar introduction, java step by step, java beginner, jar beginner, java how to, jar how to, java step by step by raghav
Id: QB19Wqimkq4
Channel Id: undefined
Length: 10min 58sec (658 seconds)
Published: Sat Oct 22 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.