(upbeat music)
- When you compile Java code,
you're creating bytecode files.
These files have a file extension of .class.
So your primary code file would be main.java
as shown up here in the tab,
and that will result in a file called main.class.
You can compile your applications
either from the command line or from within IntelliJ.
I highly recommend using IntelliJ IDEA for the most part
but it's worth going through one exercise
from the command line
so you can see clearly what's happening.
Notice first of all, that my main.java file
is in a sub-directory structure called com.company.
It looks like a package right here
but I'm going to go to the settings in the project window
and I'll deselect compact middle packages.
And that opens up the tree so you can clearly see
where the file is stored.
Now I'll go to terminal.
In terminal, I'll start off at the root directory
of my project.
And if I type ls on Mac or Linux or DIR on Windows,
I'll see the contents of that directory.
I'll navigate to the source directory
because that's where my code is.
And once again, I'll type ls
and I see the com sub-directory.
Now, regardless of how deeply nested your Java files are,
you always want to compile from the source directory
when you're working from the command line.
And here's how you compile.
I'll type javac, and then I'll address the file
using the directory structure
com/company/Main.java.
If you're working on Mac or Linux use forward slashes,
and if you're working on Windows use back slashes.
Press enter and you won't see any response
on the command line itself.
But when you go back to the project window,
it should refresh automatically and you should see
the new file main.class shown there.
Now to run the file, use this syntax.
Back in terminal, I'll type java,
that's the virtual machine, and this time I'll use
package syntax, not directory syntax
with com.company.Main.
When you're addressing the class file,
don't include the .class extension.
Nothing happens.
So now I'll go back to my code and I'll add a bit of output.
Within the main method I'll type sout.
That's a template that expands when you press
enter or return or the tab key to System.out.println.
And that's how you output something to the Console.
And I'll output the string in double quotes.
"Hello from Java."
I'll come back to terminal,
I'll recompile and I'll run the code again.
And this time I see Hello from Java.
Now, the compilation process
when you're working in IntelliJ IDEA is a lot easier.
At any point you can go up to the menu and choose build
and then select build project.
And that will create an out directory.
In the out directory you'll see production,
then the name of the project and once again,
your directory structure.
And there's your main.class file.
When you're working within IntelliJ IDEA,
that's the version of the bytecode files that you'll use
to package and deploy your applications.
So, simple Java files are just text files,
.class files are bytecode files.
You're in charge of making the text file
and the Java compiler creates the class files for you.
(upbeat music)