Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Local Development Environments!
You have completed Local Development Environments!
Preview
The JVM is the runtime engine of the Java Platform, which allows compiled Java bytecode to run.
Handy Links
Newly Added Acronyms
- WORA - Write Once Run Anywhere - Java can be compiled into bytecode and run on any device that has a JVM.
- JIT - Just In Time compilation - A final compilation step that converts bytecode to native machine code during runtime startup
Acronym Glossary
- SDK - Software Development Kit - A grouping of tools that allow you to create software locally. Also some times referred to as devkits.
- JDK - Java SE Development Kit - A set of tools specifically for developing Java SE Applications
- Java SE - Standard Edition
- Java SE API - Application Programming Interface - A set of libraries provided to build applications.
- JCL - Java Class Library - A synonym for the Java SE API. More info here.
- JVM - Java Virtual Machine - an abstract computing machine.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We have already talked briefly
about compiling,
0:00
and we compiled
every time we changed our code.
0:03
But we haven't really talked
about exactly what it was doing.
0:06
When we run the javac command,
we are converting the code
0:11
we wrote into something
called Java bytecode
0:14
and storing it in those class files
that are generated.
0:18
Now, you might not realize this, but
0:22
you can pass those class files to anyone.
0:24
The person receiving your files
could be running on Windows or a Mac
0:28
or a Unix-based system, a mobile device,
even a Wi-Fi-enabled refrigerator.
0:32
As long as they have
the Java virtual machine,
0:39
the JVM, it can run those same class
files.
0:42
Now, I realize that might not be obvious
how convenient that is, but it's
0:46
one of the main reasons for Java's initial
and continuing widespread popularity.
0:51
Java was heavily marketed
using the phrase, write once, run
0:57
anywhere, or as you might have guessed
it by now, the acronym WORA.
1:02
Most compiled
languages like C, for example, require
1:08
the user to take the source code and
compile it for each specific environment.
1:11
This can be a very tedious
and long process.
1:17
So Java, from its inception,
wanted to avoid this frustration.
1:20
and this is why they created
the Java Virtual Machine, the JVM.
1:25
There is a specification
that must be followed by all
1:29
environments that want to provide
an implementation of a JVM.
1:33
These specifications can be
thought of as a contract and are what
1:37
allows each of the different environments
to abstract away
1:41
the specific differences between itself
and other environments.
1:44
Because each of these JVM implementations
is specific to the environment,
1:49
they can be tailored
and tuned specifically for the system.
1:54
Many advances have been made in the JVM
arena since the beginning.
1:58
In a lot of the cases, due
to a method known
2:03
as just-in-time compiling, or JIT,
2:06
each JVM can basically run code
almost as fast as natively compiled code.
2:09
Now, don't worry too
much about the nitty-gritty details here.
2:15
I just wanted to get these terms
in front of you.
2:19
as we're going to start encountering them
as we progress through this course.
2:22
I've included links in the teacher's notes
if you want to dive deeper.
2:27
Let pop into a workspace and take a quick
2:31
look at a few JVM
related things Now let explore
2:34
some JVM specific details
by looking at system properties
2:38
Our workspace is set up with a package
2:43
com.teamtreehouse and we have a class called systemizer
2:45
to help us inspect properties
of our JVM instance.
2:49
For example,
we can print out the class path
2:53
with system.out.printf.
2:56
This is the class path.
3:01
percent s, percent n,
3:03
then we'll put a comma, and on the next
3:07
line, system.getproperty, and inside the
3:09
parentheses, java.class.path.
3:14
Okay, so
3:18
a class path is
what is used to find the name of the class
3:20
you are talking about
when you run the java command.
3:23
Now, it might not be clear,
but when you run the Java
3:27
command, you're actually passing
the name of the class.
3:30
Let's examine
that by running this application.
3:34
In the terminal,
I'm going to type clear and and javac
3:37
com slash team treehouse
3:42
slash systemizer dot java and and java
3:45
com dot team treehouse dot systemizer.
3:51
Remember, when you run the Java
command, you pass the name of a class
3:54
using dot notation
that contains the main method.
3:59
The JVM then searches the class path,
4:04
which by default is the current directory
denoted by the period.
4:06
If you change directories, for example,
into the temp directory
4:11
with cd slash tmp and run
4:16
java space com dot team treehouse
dot systemizer,
4:20
it won't work because the dot class files
aren't in that directory.
4:25
However, one thing you can do
is explicitly set the class
4:31
path using the dash cp option.
4:34
For instance, we can set the class
path to our home directory with the tilde
4:38
and forward slash workspace, followed
by the name of the class in dot notation.
4:43
com.teamtreehouse.systemizer.
4:50
Notice how
4:55
the class path is now
set to that directory.
4:55
What other properties
do we have access to?
4:59
Let's go figure that out.
5:02
Maybe we can do a little bit of review
along the way.
5:04
The system class offers
access to a method called getProperties
5:07
which returns a properties object
5:12
This object is essentially a map
5:15
that contains all system properties
5:18
Let check the documentation for the system
class properties
5:21
Okay, so properties extends hash table,
5:27
which extends dictionary,
which of course extends object,
5:30
and it implements a map.
5:35
Let's take a look and see if there's
anything else on this properties
5:38
method that we might want to implement.
5:41
Down here,
there's a string property names,
5:46
which returns
a set of keys from the property list.
5:50
That sounds like something we might want.
5:54
So let's go ahead
and use the string property names method.
5:56
All right, back to our code.
6:00
We'll first need to import set with
6:02
import java.util.Set.
6:05
Then we can use
6:11
set angle bracket string
close angle bracket
6:12
prop names equals system.get properties.
6:16
That will return the properties object,
6:20
which has the string property names
method attached to it.
6:23
So we can add .string property
6:27
names parentheses to the end of that line.
6:30
We'll now have access
to a set of the property names.
6:33
So let's go ahead and do a for each loop
to loop through them all.
6:38
We'll say for each property
name in prop names.
6:42
So that variable property name
that we'll get back is a set.
6:47
And we'll say system.out.printf%s is %s%n.
6:52
Then we can print out the property
name and get the value.
7:01
We'll just get it from system.getProperty,
7:06
like we used earlier to print the class
path.
7:09
We'll pass in the property
name variable through the loop each time.
7:13
There you go.
7:20
Let's save the file
so we can run our application again.
7:21
First, let's change back to the workspace
directory,
7:25
cd tilde slash workspace.
7:28
Now, I'll press the up arrow
7:34
until I get back to the command chain
starting with clear.
7:36
Whoa, okay, that's a lot of properties.
7:40
Scrolling is a bit tight here.
7:43
There is one command line
trick that I want to show you.
7:46
If you put a pipe
at the end of a statement
7:49
and pipe it into a program called less
7:52
it will pause the scrolling output
and you can navigate it with the up
7:55
and down arrows Press Q to quit less
and return to the command prompt.
8:00
Much better.
8:06
As you can see though,
the list of properties are not
8:07
in any particular order, so it can be hard
to find what we're looking for.
8:10
Luckily, there's a set implementation
that does alphabetical ordering.
8:15
Remember that you can pass
most collections
8:20
into the constructor
of the implementation.
8:22
Do you remember what I'm talking about?
8:26
Okay, go ahead and pause me
and try to put the proper
8:28
ordering on set implementation in place.
8:32
All right, give it a try.
8:35
Okay, here you go.
8:38
First, let's import tree set,
8:40
which gives us the alphabetical sorting
and simply right here
8:42
we just pass
what we have there already into it.
8:47
Then save the file and run it again.
8:55
Excellent!
9:02
TreeSet is sorting
the property names alphabetically,
9:03
which will be helpful
when scanning through them.
9:07
Let's do a quick
graphical review of what we just learned.
9:10
So we write code wherever we want to
and save it into the file.
9:14
Because we have the JDK installed,
we're able to run the compiler
9:19
using the javac command against that file.
9:23
That creates dot class files
9:27
that live alongside the original dot
java file.
9:29
These dot class files are Java bytecode
9:34
and can be taken and run on any JVM
or Java virtual machine.
9:37
One way this running is done
is by using the Java command and passing
9:43
the name of a class
containing a static method
9:48
named main as the first argument.
9:51
The Java program takes a class
name and looks in the defined class path.
9:54
To launch an application
in the JVM instance, most likely
10:00
that JVM will use just-in-time compilation
or JIT
10:04
to further compile
that code down to native machine code.
10:08
Each JVM
implementation is specifically written
10:13
for the environment that it's running on
by following a specification.
10:16
Therefore, it has special knowledge
about how to be as performant as possible.
10:21
Now that we have
10:27
the terms, let's get things
working locally on your computer.
10:28
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up