So continuing on with our discussions about packages.
The other thing you can actually do what you're creating a class
is you can actually specify a package and will actually make that package for
you automatically at the same time as creating the class.
Come up here to file, new, create Java class.
Normally I would just type player And that should carry that
to org.example.mypackage, because I'm actually in that area.
I've got clicked, so I click OK.
You can see it's automatically created that class in that folder,
in that package.
I'm gonna undo that.
I can actually do that again, though.
File -> New -> Java Class, and this time, if I type a package name,
com.example .game.Player for arguments sake.
Click on OK with that.
And notice it's actually created a package sort of within a package
org.example.mypackage.com.example.game.
That's clearly not what we want in this particular case because
we wanted to create a package Which was just com dot example dot game.
So it's important to, if you have got something clicked here to the left,
to probably click or to definitely click source.
So I'm going to undo that again, and then come back to source this time or
I can make sure, that's probably the easiest way is to do that.
And then click on file, new, java class, and now if we type com dot
example dot game dot player It correctly creates the new package
com.example.game and puts the new player in there, the new player class in there.
So what we're going to do now is we're going to use a few classes from our
interfaces challenge.
So we're going to specifically use the player class, and I know I've already
created the player class, but also the monsters class in the isaliable interface.
We're gonna overwrite the Player class as well.
So we're gonna go back to the source code for the InterfacesChallenge and
if you haven't got this already.
If you haven't typed it in yourself, go back and
you can actually download the source code from the course itself.
There's actually links there so you can grab this source code.
So I'm going into InterfacesChallenge.
Go into my source folder, and down to and the first one I'm gonna select is monster.
Now you might think you can actually just select it and drag, and it does seem like
it's going to work, and you can see it's highlighting in red the package it's going
to put it to, going to place it, but the thing is that's a move.
And by moving something you're actually deleting it from the source folder.
So, probably don't want to do that.
You don't want to delete the code that's in the current interfaces challenge.
Because that code will then no longer work.
Because that file won't be there anymore.
So, a better option is to come back to the finder.
And again, you'd Windows explorer or whatever your explorer is for
your operating system.
Select Copy, and Copy will be same for all operating systems.
There's a version there for you.
So select Copy, come back to the package you want and paste.
This time you can see the message is Copy, the name.
It's giving us the full name and
ultimately that'll be the class name as well.
And you can see that the folder it's pointing it to matches the package com
example game, com example game.
And we click on OK.
And it's been added successfully.
We getting an error here because, of course,
we haven't yet got the savable interface, but we're going to do that next.
So I'm gonna go back and do the same thing for that, our savable copy.
Back to here, select the package.
Right-click, paste, and we're gonna click on OK.
That's now added the interface.
And that leaves us with the Player class.
Now currently that's however empty.
And what we're going to do is do the same thing, we're gonna go back to Player.
Right click, copy.
Back here to com, go to example.game, paste.
And yes, we're going to use the same name.
This time we get a dialog pop up to tell us that it already exists.
Do we want to overwrite it?
Or do we want to skip it?
We want to override it because we want the version that came from
the interface challenge so click on okay and now we've got our three files.
Just confirming, when it's doing this it will actually fix the package name so
if the old source code has a different Package, when you're actually
copying it into here IntelliJ is actually changing the package in this code for you.
So it's quite handy how it's doing that for you.
So what I'm about to do is create a package for these three files,
the Player.javamonster.java and [INAUDIBLE] .java, So that we can
actually use those in another project, but before we do that we should actually
update our main class and put in the test code that comes back from the challenge,
so that we can actually test to make sure that things are working as they should.
It's always a good idea before you create a jar file, which is what we're doing,
a library, Is to make sure that the code is obviously working and tested.
So, we'll go back one more time to our finder.
I'm going to select main, I'm going to copy that.
And we basically are gonna go in here to [INAUDIBLE] example.game, and
I'm gonna paste it in there.
And I'm going to overwrite the existing main.java.
Actually, there wasn't a main.java.
Cuz, of course, I was thinking of the main.java down here,
on org.example.mypackage.
So if Dale got four classes in our common.example.game.
And what I am going to do now is just remove this other package,
this org.example.mypackage because we no longer need that anymore.
So I am going to right click that, delete.
And I'm just going to double click mine which is already selected.
And it will just run that to make sure that it's actually And
you can see we got the same output we got from the challenge.
The interface challenge so the codes obviously okay, there's no problems.
We're not getting any compile errors and you would
obviously do your normal testing at this point to make sure that actually worked.
Assuming that's working, what we're gonna do now is delete the main class.
We're going to delete that and the reason we're doing that is we're going to be
importing this project, these three files, effectively into a [INAUDIBLE] file
that will then be imported into another project all together and
that project will have its own main class.
So, we're specifically trying to include only the code
relating to the actual game itself.
So now that we've actually removed the other examples I'll actually remove this
[INAUDIBLE] As well,
that shows up for you to delete that as well because we don't need that anymore.
We're left now just with the content example dot game, package, and
the three files.
So, what I wanna do next is go into file and select project structure.
What I wanna select is artifacts.
We're going to create a new artifact so we're gonna click on the plus button.
We're gonna click on jar, cuz jar stands for java archive and
that's sort of like a zip file but for java code.
We're gonna select that and come over here and select from modules with dependencies.
When I do that, you should see that package demo came up automatically
as the module.
Module is basically the project in this case.
The package [INAUDIBLE] although you can't have moddable modules in a single project.
In our case, we've only got just the one.
Now if you wanted to make the Java file executable, you'll specify main class.
But we don't want to do that so we're going to leave that blank.
We're also going to extract to the target Java as well.
I'm going to click on okay to that.
This actually gives us a folder.
If you have a look in this to the end of that.
This actually shows us that this current project slash out slash artifacts slash
package demo underscore jar which is the folder name.
That's where it's going to actually create this jar or PackageDemo.jar.
So make sure you actually know where it's gonna be creating that while having a look
at the app directory.
Once you've done that, click on OK.
That's now specified the folder and we can actually just go ahead and
we can actually make the project.
We could also at any time build artifacts.
And build artifacts enables you to actually build this.
So I've packaged You can see that we got just a clean, edit, rebuild.
I'm just gonna click on build.
And very quick process, you probably saw something briefly down at the bottom
left hand corner of the screen, that jar file has now been created.
And just to confirm if you go back now to our finder, to our navigator and
actually have a look, and look now in package demo,
because we finished with interfaces challenge for now, Package demo out.
It's no in the artifacts folder that's been created.
Package demo I'm just going to go draft, folder, and
there's our package demo dot Java there has been created by that build prices.
At this point, what we're going to do now is we're going to actually create a new
project and we're import that lobry and make sure it still runs.
So I'm going to click on file, new, select project.
Click Java and next do what we normally do.
Create a command like that.
We'll call this one Game Test.
[NOISE] Into the base package line.
Click on finish until it's asking us do we want to close this project down?
We'll have two away because when we click on this window.
Because we don't need the package demo anymore.
So I click on that.
I've now got a very base packages as you can see there.
Base project I should say.
Now at this point what we can now do is we can actually import that.
We'll take a look at the external libraries, you can see there's
a heap of libraries that are piled with the Java ones that we have talked about.
But our library's not in there yet, obviously,
because we haven't actually imported it.
So what we're gonna do now in this new project,
where we want to actually use the Java that we've created.
Click on file, go into project structure.
Then we want to go into libraries.
And click on plus.
And we're gonna specify that's a new project library, a Java one.
And we need to specify where that is.
I'm gonna go down to our folder.
We created it which wa in PackageDemo > out > artifacts, and that one there.
You can see it's a PackageDemo.jar, which is in the PackageDemo_jar folder that was
created when we had the other project open.
Click on OK And it actually tells us that the library PackageDemo will be added to
the selected modules, GameTest, which is the name over here of our current project.
We do want that, so we're gonna click on OK to that, then we click on OK.
So now that we've actually done that, we External Libraries,
we've now got another option here, we've got PackageDemo showing as a library.
And if we expand to that, you can see we've got our package com.example.game And
i've also got two classes there.
Once in player, and we need to face our savable.
So, now that we've actually done that you can presume the code is there
executable code to actually run this so
what we can actually do is add some code to our actual java.
And we should be able to run this now and
instead of the previous interface has changed, when we're running the code that
was part of this project, we'll be running the code that's really embedded or
included in that packaged mo.jar so the easiest way to do that is just to go back
to our interface challenge and what I'm going to do is copy the main.
And I'm going to actually paste it.
Actually I'm going to close down the external libraries, open up our project.
I'm going to paste it in com.timbuchalka, which will overwrite the existing one.
I'm going to do that, and confirming yes, we do want to override it.
Now did you see what happened there?
There was a brief delay where the import statements weren't there and
then they actually showed up automatically.
And again, they're automatically coming up because of the way we've configured our
preferences, which I showed in, not the last video, I think it might have been
the video before, but an earlier video in this section, in any case.
It's automatically found these three classes this time in the external library,
packaged demo as you can see there.
And that's why that we're able to run it.
So if we actually then run this, we're still getting the same results.
The code is being executed, but of course,
looking at our project We've only got a main class, in [INAUDIBLE].
We haven't got those other classes,
because they're actually coming from this package.
So that's how to go about actually adding a library,
or creating a library as you saw, and then adding it to your code.
And that's a good idea if you want to have some code that you reuse for
multiple projects.
You create a library like we've done there.
Then you import it using the mechanism that I showed you.
And then [INAUDIBLE] will actually do the rest for you, as you saw there.
So that's it for the package discussion.
In the next video we'll go ahead and actually do a package challenge.
So I'll see you in the next video.