Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The Java Virtual Machine11:15 with Craig Dennis
The JVM is the runtime engine of the Java Platform, which allows compiled Java bytecode to run.
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
- 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
- JRE - Java Runtime Environment - A minimum set of tools that allow local Java programs to execute
- 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.
We have already talked briefly about compiling, and 0:00 we compiled every time we changed our code. 0:03 But we haven't really ever talked about exactly what it was doing. 0:05 When we run the javac command, 0:09 we are converting the code we wrote into something called Java bytecode and 0:11 storing it in those .class files that are generated. 0:15 Now you might not realize this, but you could those .class files to 0:19 anyone who is using the Java Runtime Environment, or JRE. 0:22 That person receiving your files could be running on Windows, or a Mac, or 0:26 a UNIX-based system, a mobile device, the Wi-Fi-enabled refrigerator. 0:30 As long as they have the Java virtual machine, or JVM, 0:35 it can run those same class files. 0:37 Now, I realize that that might not be obvious how convenient that is, but it's 0:41 one of the main reasons for Java's initial and continuing wide spread popularity. 0:45 Java was heavily marketed using the phrase write once, run anywhere, or 0:50 as you might have guessed by now, the acronym, WORA. 0:55 Most compile languages, like C for example, require the user to take 0:59 the source code and compile it for each specific environment. 1:03 Now this can be a very tedious and long process. 1:06 So Java, from it's inception, wanted to avoid this frustration and 1:11 this is why they created the Java Virtual Machine, or JVM. 1:14 There's a specification that must be followed by all environments that want to 1:19 provide an implementation of a JVM. 1:22 These specifications can be thought of as a contract and 1:26 are what allows each of the different environments to abstract away 1:29 the specific difference between itself and other environments. 1:32 And because each of these JVM implementations is specific 1:36 to the environment, they can be tailored and tuned specifically for the system. 1:39 Many advantages advances have been made in the JVM arena since the beginning of time. 1:43 And in a lot of cases, due to a method known as just-in-time compiling, or JIT, 1:48 each JVM can basically run code almost as fast as natively compiled code. 1:53 Now don't worry too much about the nitty gritty details here. 1:59 I just wanted to get these terms in front of you, 2:02 as we're going to start encountering them as we progress through this course. 2:04 I've included links in the teacher's notes if you want to dive deeper. 2:09 Let's pop into a workspace and take a quick look at a few JVM related things. 2:12 All right, so the workspace is set up with the package of com.teamtreehouse and 2:17 there's a file called Systemizer. 2:21 We'll use this class to explore properties available that are specific 2:24 to our JVM instance. 2:27 So, the system class that we use all the time has a method on it and 2:30 it can access those properties. 2:33 Let's take a look at that. 2:34 So, let's go ahead and we'll print out the classpath. 2:36 So, we'll say This is classpath. 2:42 And we'll use our %s, and let's do a %n while we're in there. 2:47 And so System.getProperty, 2:53 and the name of the property is java.class.path. 2:55 Okay, so a classpath is what is used to find the name of the class you are talking 3:02 about when you run the Java command. 3:07 Now, it might not be clear, but when you run the Java command, 3:10 you're actually passing the name of the class. 3:13 Let's examine that by running this application. 3:16 And remember, it's in a package. 3:18 Now normally, ours are in the root, so, let's run that. 3:19 We'll say, clear, and we'll compile it. 3:24 So we need to compile the actual file. 3:27 And, now we're gonna pass the name of the class. 3:32 And remember, cuz it's the name of the class it uses dot notations, 3:35 com.teamtreehouse.Systemizer. 3:39 So like I mentioned, it uses the classpath to look for our classes. 3:42 So note here, how our class path just contains a period. 3:48 Now what that means in UNIX speak, is look in the current directory. 3:52 Now, if I move to a different directory, like say temp on this machine. 3:56 So, I'm gonna change my folder, and then I'm gonna try to run that same command. 3:59 So I'm gonna say, java com.teamtreehouse.Systemizer, and 4:04 it shouldn't be able to find it, right, because I am not in that directory and 4:09 our classpath by default is dot. 4:14 So it says it could not find or load the main class. 4:17 So what you can do is actually force the classpath with a new parameter. 4:20 And that parameter is cp, or you could spell out class path as well. 4:24 So I'm gonna point it back to see where it says there ~? 4:27 So that means home directory of the current user workspace. 4:30 So we're gonna say run the Java program using this classpath. 4:35 And then I'll again the com.teamtreehouse.Systemizer. 4:38 That should work, and now see how it says the classpath is now that cuz we 4:44 forced that to be set through the settings. 4:48 Cool. 4:50 So the class path will look in directories but 4:50 it can also use something called a java archive or a jar file 4:53 which is essentially a zip file of java classes and some additional information. 4:56 We'll check out jars here pretty soon but I just wanted to troll you with yet 5:01 another new acronym. 5:04 Sorry about that. 5:05 So what other properties do we have access to? 5:06 Let's go figure that out. 5:08 Maybe we can do a little bit of review along the way. 5:09 All right. 5:12 So the system has a method called getProperties. 5:12 Let's check the dock. 5:15 So let's go search for System getproperty 8 cuz we have Java 8. 5:17 So here it is, this first document here. 5:24 So we were using here, we're using this getProperty. 5:28 And there is another method here called getProperties, and 5:34 it gets all of the properties. 5:38 Let's take a look at that. 5:40 Okay, here they are listed in the documentation of what they all are. 5:42 Let's see what they are here, locally. 5:45 So it returns a Properties object. 5:46 So let's click into that. 5:48 And a Properties object, let's see. 5:50 It extends hash table which extends dictionary, which of course, 5:52 extends Object and it implements a map. 5:55 So maps, they have get and set. 5:57 Let's take a look and 6:00 see if there's anything else on this Properties method that we might wanna do. 6:00 Oh, cool. So look here you can say, 6:04 get a property and you give it a default value in case it doesn't exist. 6:06 That's kinda handy to know. 6:09 Here, down here, there's a stringPropertyNames, 6:12 returns a set of keys. 6:15 So we know set, that's a unique thing that's pretty great, 6:16 that's kind of what we want. 6:19 So let's go ahead and lets use the stringPropertyNames. 6:20 All right, so lets pop back to our code, 6:24 and we'll say Set<String> promptNames 6:29 = System.getProperties. 6:36 Again, that returns the Propertie object which has 6:40 string operating names on it ,right, so we just chain that. 6:44 Actually, I forgot to call the method. 6:49 Method, call getProperties, stringPropertyNames, and also, 6:52 we need to import the set, right? 6:56 Remember we need to go import 6:58 java.util.Set;. 7:03 So, we now have a set of the property names. 7:08 So, let's go ahead and we'll a do a for each loop. 7:11 We'll say for each propertyName in propertyName. 7:13 So, that ariable that we just got back which is a set, and we'll say 7:20 System.out.printf("%s is %s, 7:23 put a new line in there as well. 7:33 So then what we'll do is we'll print out the propertyN ame, 7:35 and then let's get the value. 7:38 We'll just get it from System.getProperty, which we used earlier about there, right? 7:40 And we'll pass in that variable that's coming through the loop each time, 7:44 which is property. 7:46 Cool. 7:51 Put in that for loop. 7:52 All right. So, let's pop down here and 7:53 let's run that again. 7:55 Let me get into my workspace. 7:56 And we press the up arrow until I get back to the clear statement. 8:02 There we go. Let's run that. 8:05 I forgot to save. 8:09 Let's do that again. 8:10 Whoa, okay. 8:14 So scrolling is a little bit tight here. 8:15 So there's one trick that I wanna show you that's on the UNIX command line. 8:18 If you put a pipe at the end of a statement and pipe it into, 8:23 there's a program called Less. 8:26 Now I can use the up and down arrows. 8:30 Check the teacher's notes for that. 8:33 Okay, the order is a little bit out, but we're definitely seeing that this is that. 8:34 So there is a set implementation that does alphabetical ordering. 8:40 Remember that you can pass most collections into the constructor of 8:44 the implementation. 8:47 Do you remember what I'm talking about? 8:49 Okay. 8:51 Go ahead and pause me and 8:51 try to put the proper ordering on set implementation in place. 8:53 All right, paused? 8:55 Okay, here we go. 8:59 So let's import this TreeSet, that is alphabetical sorting. 9:01 And, it's simply, 9:08 right here, just pass what we have there already into it. 9:11 So, we'll do a new TreeSet, and we'll pass that in. 9:15 All right, to get out of less, if you press Q, let's go ahead and 9:23 run that one more time. 9:27 Here, we go. 9:29 There it is. 9:30 Nice alphabetical order. 9:31 So, look here, it says the file separator is forward slash. 9:33 Now, if I was doing this JVM locally on my Windows machine, 9:36 it would probably say backslash, right. 9:39 So, there's all sort of different things in here an you should feel free to use any 9:42 of these properties in your code. 9:45 Look around in here and you can look at that documentation that we saw and 9:46 see how they match up. 9:50 It's a pretty interesting thing here, and I hope you enjoyed that review. 9:51 Feel free to use these properties anyplace in your code that you want to be dynamic 9:54 based on what the JVM is. 9:58 So, let's do a really quick graphical review of what we just learned. 10:01 So we write code wherever we want to and we save it into a file. 10:05 Because we have the JDK installed, 10:10 we're able to run the compiler using the javac command against that file. 10:12 That creates .class files that live alongside the original .java file. 10:16 These .class files are Java bytecode and 10:21 can be taken and run on any JVM, or Java Virtual Machine. 10:25 One way this running is done is by using the Java command and passing the name of 10:30 a class containing a static method named Main as the first argument. 10:34 The Java program takes a class name and looks in the defined class path. 10:39 To launch an application in the JVM instance, 10:45 most likely that JVM will use Just In Time compilation, or 10:48 JIT, to further compile that code down to native machine code. 10:52 Each JVM implementation is specifically written for 10:57 the environment that it is running on by following a specification. 11:00 Therefore, it has special knowledge about how to be as performant as possible. 11:05 So, now that we have the terms, let's get things working locally on your computer. 11:11
You need to sign up for Treehouse in order to download course files.Sign up