How to Create a Java JAR File and Import Package Libraries into a Java Package - APPFICIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna learn how to export your Java code into a jar file and how to import it into another Java program and use it to create objects from the classes that you have so here we have a class called person and we also have a class called student which extends person and let's put these in in a different package other than the default package so I could just rename rename this by singer refactor these file these files are inside the default package but if you wanted to export these files so that other people can use them you should move them out of the out of the default package you should move them out of the default package and into package with the name so let's create a new package by right-clicking on source and saying new package now let's call this adapter - hold on that's usually your domain name or a company domain or whatever you want it to be an official com is going to create a folder called official and then another folder inside it called come and and inside the comp older it's gonna contain all your class files so click OK and there's the new package I'm going to take student in person and move them out of the default package into the App official package and you can see that it added a package statement at the top of both of these classes the person and student so now check this out I'm gonna right click on my package and say export select Java jar file which stands for Java archive file is basically a zip file that contains your Java classes select next and I'm going to select the export destination to my desktop right there and give it a name call this my classes click Save and finish and then it just exported them into the jar file let's go to the desktop and verify it here are my classes that jar and just to verify that inside it is only the class files I'm going to extract it with winzip to this folder just so you can see what's in cited there's my classes there's a epiphysial folder that I mentioned here's the dot-com folder and inside it are the two class files not the source code that Java file is just the class files let's delete this we don't need this and let's just work with just this jar file now what can I do with this well you can so you can take this and you can give this to anybody and they could start creating a person and student objects using just this jar file and in addition you may also want to create a Java API like writing Java comments for all the methods and then running the Java doc command tool but that's a different video that you can watch on how to do that and in this video this is what we're gonna do we're going to delete I'm just going to leave my entire Java project so that I don't have the source code anymore it's all gone and I'm going to create a new Java project and it's empty the source folder so we it's still there I'm gonna delete this bleeped firstly there's delete and while my sure I want to delete it okay deleted so in the source I'm gonna create a new class let's call this demo and put main method in there and there it is and let's see if I can start creating person and student objects person P new person and doesn't work student s is a new and that doesn't work also getting errors let's see what the quick fix says the quick fix says to create a class it doesn't give me an option to import the class because it doesn't know where it's located it can't find those person in student classes so this is what you need to do in eclipse is to add them to your class path so let's go to your Java project right click on it and select build path and bigger build path select libraries and you could go and click it add external jars and then look for it on your desktop which I don't recommend you do because if you were to take this code and move it to a different system later the jar file wouldn't travel with you so what you should do is just right click on your java package and say new folder you can call it live for library let's just gonna contain all your jar files so let's go back to the desktop and copy this file or I'm just gonna cut it actually and paste it in the lid folder so there it is and I still don't get the option to import it it still can't find it in the demo class so let's go back and try that again all right click it the Java package build path configure build path select libraries and click Add jars rather than add external jars and then you can browse your directory structure find the Lib folder select your jar file my class is that jar click OK and apply apply in close and now I've added to my class path but it looks like I'm still getting errors but now check this out I can hover the mouse over a person and now I'm getting an option to import it from the class person which comes from official comm select the quick fix do the same thing for student and my package statement now became my import statement official comm dot the class name and now I can create person and student objects a student dot set name james student that set age hundred and student that set student ID community these numbers and you can see that everything is working and the only thing that would have made this better is if I was to hover the mouse over and read some description of what the method does that's why you should add Java doc style comments to all your methods constructors and public fields as well so that's a different video for you to watch and that's pretty much it for import import statements and jar files and creating packages in Java so thanks for watching you
Info
Channel: Appficial
Views: 39,459
Rating: undefined out of 5
Keywords: appficial, java, inheritance, java inheritance, java jar, jar file, how to make a jar file, export jar file, import jar file, import java libraries, java api, javadoc, eclipse, eclipse build path, eclipse jar file, create a jar file in eclipse, import a jar in eclipse, java package, import java package, export java package., java tutorial, java industry, java programming, learn java, easy java, jar example, package example, java export, java eclipse, java 10
Id: w7dOS8KBSEU
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Wed May 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.