1 00:00:00,230 --> 00:00:04,490 To provide motivation for the next option, let's look back at our source directories. 2 00:00:04,490 --> 00:00:06,730 Earlier, we noticed that upon compiling, 3 00:00:06,730 --> 00:00:10,760 our class files were placed in the same directory as our source files. 4 00:00:10,760 --> 00:00:14,311 Here we see Cheese.class right next to Cheese.java, and 5 00:00:14,311 --> 00:00:17,340 Main.class right next to Main.java. 6 00:00:17,340 --> 00:00:20,850 Now, since we're developers, chances are that we're creating software 7 00:00:20,850 --> 00:00:23,580 that will be distributed and installed elsewhere. 8 00:00:23,580 --> 00:00:28,130 What we'll distribute though, are the .class files not the .Java files. 9 00:00:28,130 --> 00:00:33,140 We will code the .Java files and the compiler will produce the .class files. 10 00:00:33,140 --> 00:00:36,130 In an effort to preserve this separation of concerns and 11 00:00:36,130 --> 00:00:40,500 keep our directories tidy, let's stick the .class files elsewhere. 12 00:00:40,500 --> 00:00:44,315 We can accomplish by using another command line option, the directory or 13 00:00:44,315 --> 00:00:47,187 -d option with the javac command. 14 00:00:48,780 --> 00:00:51,880 Now, if you've already switched to an IDE such as IntelliJ or 15 00:00:51,880 --> 00:00:56,000 Eclipse, you've noticed that those IDEs automatically create a directory for 16 00:00:56,000 --> 00:00:57,870 you that contains the .class files. 17 00:00:58,910 --> 00:01:02,000 Likely it's named bin, short for binary. 18 00:01:02,000 --> 00:01:03,640 Out, short for output. 19 00:01:03,640 --> 00:01:08,020 Or build, referring to compiling, or building, our application. 20 00:01:08,020 --> 00:01:12,252 Let's create one now called out, and put all of our .class files there. 21 00:01:12,252 --> 00:01:17,122 So, I'll right-click on the workspace directory and choose New Folder, 22 00:01:17,122 --> 00:01:19,260 and name it out, all lower case. 23 00:01:20,320 --> 00:01:25,559 Great, before we compile to the out directory let's be sure to delete our 24 00:01:25,559 --> 00:01:31,407 existing .class files so we can ensure new ones are created in the out directory. 25 00:01:36,508 --> 00:01:39,420 Now we're ready to compile to the out directory. 26 00:01:40,650 --> 00:01:45,924 To do this after javac, I'll specify the directory option using -d and 27 00:01:45,924 --> 00:01:52,250 tell the compiler to use the out directory as the destination for our .class files. 28 00:01:52,250 --> 00:01:55,189 Again, we won't forget to specify our class path. 29 00:02:01,848 --> 00:02:07,172 If we refresh the out directory, we will see our new class files 30 00:02:07,172 --> 00:02:12,610 that were just generated as a result of our compiler statement. 31 00:02:14,080 --> 00:02:14,819 Let's check it out. 32 00:02:14,819 --> 00:02:17,320 If we drill down all the way to the override folder. 33 00:02:17,320 --> 00:02:21,990 There is Cheese.class and Main.class. 34 00:02:21,990 --> 00:02:26,370 This is really nice as it keeps the directories of our source code clean. 35 00:02:26,370 --> 00:02:30,220 We're not littering them with files that we'll never directly edit. 36 00:02:30,220 --> 00:02:31,950 Now if we'd like to run the application, 37 00:02:31,950 --> 00:02:36,102 we'll specify the class path as the out directory. 38 00:02:36,102 --> 00:02:40,055 java -cp is the out directory, and 39 00:02:40,055 --> 00:02:46,100 com.teamtreehouse.override.main is the class that we'd like to run, 40 00:02:46,100 --> 00:02:47,570 and there's our output again. 41 00:02:47,570 --> 00:02:48,850 Cool! 42 00:02:48,850 --> 00:02:51,430 From now on, any time we want to compile from the console, 43 00:02:51,430 --> 00:02:54,500 we'll definitely use this approach to keep our directories organized. 44 00:02:55,540 --> 00:02:59,250 Let's check out one more command line option while compiling. 45 00:02:59,250 --> 00:03:03,550 You can ask the compiler to give you more information than what it does by default. 46 00:03:03,550 --> 00:03:05,830 When you compile using the javac command, 47 00:03:05,830 --> 00:03:08,310 you're going to be told mainly about errors. 48 00:03:08,310 --> 00:03:12,750 That is, things that will prevent your code from successfully compiling. 49 00:03:12,750 --> 00:03:15,960 Beyond errors, you can ask the compiler to tell you about warnings, 50 00:03:15,960 --> 00:03:19,030 much like we saw with the deprecated annotation. 51 00:03:19,030 --> 00:03:21,740 For this, you'll need to add a command line option. 52 00:03:21,740 --> 00:03:25,580 In general, you'll add what's called the -Xlint option. 53 00:03:25,580 --> 00:03:29,970 Lint is an old term that meant to flag suspicious behavior in your code before 54 00:03:29,970 --> 00:03:31,720 you compiled it. 55 00:03:31,720 --> 00:03:33,750 That seems to make sense here too. 56 00:03:33,750 --> 00:03:35,780 Let me add a statement to the Main class, 57 00:03:35,780 --> 00:03:39,449 and I'm sure you'll be able to spot the problem even before compiling. 58 00:03:42,029 --> 00:03:49,050 Clear my console and let me add this statement here. 59 00:03:51,770 --> 00:03:54,578 Let's see what happens when we compile and run this code. 60 00:03:58,439 --> 00:04:02,520 Compile the code and it looks like we have no compiler errors, so 61 00:04:02,520 --> 00:04:04,039 let's run this thing. 62 00:04:07,978 --> 00:04:11,320 We get our output, but then we get an error or exception. 63 00:04:11,320 --> 00:04:15,290 And we see, even if we don't understand all the gibberish that's here, 64 00:04:15,290 --> 00:04:18,780 we see that it's a / by zero error. 65 00:04:18,780 --> 00:04:20,980 And you already knew that was going to happen. 66 00:04:22,070 --> 00:04:27,700 Let's add the excellent option to compilation to display all earnings and 67 00:04:27,700 --> 00:04:29,300 see what happens. 68 00:04:29,300 --> 00:04:34,780 In order to do that, while compiling we'll use the -Xlint option, 69 00:04:34,780 --> 00:04:37,050 don't forget to capitalize that X. 70 00:04:38,340 --> 00:04:42,460 Specify the out directory as the location for our .class files. 71 00:04:44,010 --> 00:04:47,245 Don't forget our class path and the full path to 72 00:04:47,245 --> 00:04:54,060 Main.java. 73 00:04:54,060 --> 00:04:58,090 A warning, it's a warning for a / by zero error. 74 00:04:58,090 --> 00:05:01,970 Hey, remember earlier when I said that any opportunity we get to catch an error while 75 00:05:01,970 --> 00:05:06,610 compiling, as opposed to while our app is running, is an opportunity we'll take. 76 00:05:06,610 --> 00:05:08,350 Yep, here it is again. 77 00:05:08,350 --> 00:05:10,560 And that's what the -Xlint option can do for you.