Using the Directory and Xlint Command Line Options5:11 with Chris Ramacciotti
After learning how to use command line options with the Java compiler, we now take a look at how we can use the directory option to tell the compiler where to place our bytecode (.class) files. Then, we'll see how we can turn on compiler warnings using the "Xlint" option.
To provide motivation for the next option, let's look back at our source directories. 0:00 Earlier, we noticed that upon compiling, 0:04 our class files were placed in the same directory as our source files. 0:06 Here we see Cheese.class right next to Cheese.java, and 0:10 Main.class right next to Main.java. 0:14 Now, since we're developers, chances are that we're creating software 0:17 that will be distributed and installed elsewhere. 0:20 What we'll distribute though, are the .class files not the .Java files. 0:23 We will code the .Java files and the compiler will produce the .class files. 0:28 In an effort to preserve this separation of concerns and 0:33 keep our directories tidy, let's stick the .class files elsewhere. 0:36 We can accomplish by using another command line option, the directory or 0:40 -d option with the javac command. 0:44 Now, if you've already switched to an IDE such as IntelliJ or 0:48 Eclipse, you've noticed that those IDEs automatically create a directory for 0:51 you that contains the .class files. 0:56 Likely it's named bin, short for binary. 0:58 Out, short for output. 1:02 Or build, referring to compiling, or building, our application. 1:03 Let's create one now called out, and put all of our .class files there. 1:08 So, I'll right-click on the workspace directory and choose New Folder, 1:12 and name it out, all lower case. 1:17 Great, before we compile to the out directory let's be sure to delete our 1:20 existing .class files so we can ensure new ones are created in the out directory. 1:25 Now we're ready to compile to the out directory. 1:36 To do this after javac, I'll specify the directory option using -d and 1:40 tell the compiler to use the out directory as the destination for our .class files. 1:45 Again, we won't forget to specify our class path. 1:52 If we refresh the out directory, we will see our new class files 2:01 that were just generated as a result of our compiler statement. 2:07 Let's check it out. 2:14 If we drill down all the way to the override folder. 2:14 There is Cheese.class and Main.class. 2:17 This is really nice as it keeps the directories of our source code clean. 2:21 We're not littering them with files that we'll never directly edit. 2:26 Now if we'd like to run the application, 2:30 we'll specify the class path as the out directory. 2:31 java -cp is the out directory, and 2:36 com.teamtreehouse.override.main is the class that we'd like to run, 2:40 and there's our output again. 2:46 Cool! 2:47 From now on, any time we want to compile from the console, 2:48 we'll definitely use this approach to keep our directories organized. 2:51 Let's check out one more command line option while compiling. 2:55 You can ask the compiler to give you more information than what it does by default. 2:59 When you compile using the javac command, 3:03 you're going to be told mainly about errors. 3:05 That is, things that will prevent your code from successfully compiling. 3:08 Beyond errors, you can ask the compiler to tell you about warnings, 3:12 much like we saw with the deprecated annotation. 3:15 For this, you'll need to add a command line option. 3:19 In general, you'll add what's called the -Xlint option. 3:21 Lint is an old term that meant to flag suspicious behavior in your code before 3:25 you compiled it. 3:29 That seems to make sense here too. 3:31 Let me add a statement to the Main class, 3:33 and I'm sure you'll be able to spot the problem even before compiling. 3:35 Clear my console and let me add this statement here. 3:42 Let's see what happens when we compile and run this code. 3:51 Compile the code and it looks like we have no compiler errors, so 3:58 let's run this thing. 4:02 We get our output, but then we get an error or exception. 4:07 And we see, even if we don't understand all the gibberish that's here, 4:11 we see that it's a / by zero error. 4:15 And you already knew that was going to happen. 4:18 Let's add the excellent option to compilation to display all earnings and 4:22 see what happens. 4:27 In order to do that, while compiling we'll use the -Xlint option, 4:29 don't forget to capitalize that X. 4:34 Specify the out directory as the location for our .class files. 4:38 Don't forget our class path and the full path to 4:44 Main.java. 4:47 A warning, it's a warning for a / by zero error. 4:54 Hey, remember earlier when I said that any opportunity we get to catch an error while 4:58 compiling, as opposed to while our app is running, is an opportunity we'll take. 5:01 Yep, here it is again. 5:06 And that's what the -Xlint option can do for you. 5:08
You need to sign up for Treehouse in order to download course files.Sign up