Intro to Command Line Options for the Java Compiler3:48 with Chris Ramacciotti
Since the final built-in annotation that we'll examine (
@SuppressWarnings) deals with compiler warnings, we'll need to sidetrack for a moment to discuss options we can use while compiling our Java code from the command line. In this video, we'll review the classpath or "cp" option, which allows us to specify where to find the root of our Java source packages.
Console Foundations Course
If you want to brush up on console commands, check out our Console Foundations Course. Among other skills, you'll learn how to...
- Navigate the filesystem
- Read, write, move & delete files
- Modify directory & file permissions
- Manage processes
- Search the filesystem
- Build & install software
The last built-in annotation we are going to look at is suppress warnings. 0:00 This annotation deals with warnings that the Java compiler produces. 0:04 Because warnings aren't displayed by default, we'll need to use some 0:09 extra options with the Java C command to instruct the compiler to show us warnings. 0:12 Let's look at a few useful command line options, including turning on warnings. 0:17 We'll need these options in order to fully understand the suppressed warnings 0:22 annotation. 0:25 Before we experiment though, let's talk about command line options or arguments. 0:26 In general, running a console command includes the following: number one, 0:31 specifying the command itself, for example javac, number two, 0:35 including optional arguments, and number three, 0:41 including required arguments, such as the name of the file we want to compile. 0:44 For example, you may have seen the class path, or cp option to specify 0:50 which directory to use as the root for the package names while compiling Java code. 0:56 In this command, the option name is CP and 1:02 the value we need to specify is a directory. 1:04 This allows our main class to use a package name 1:09 com.teamtreehouse.override, without SRC in the package name. 1:12 What do you say we practice this class path option and a couple more? 1:17 Let's do it. 1:23 I'm back in the same work place as project where we left off after the last video. 1:25 The first thing we'll practice is using the class path option 1:29 while compiling our code. 1:32 Let's compile the code from our override package using the class path option. 1:33 First we'll compile main.JAVA using the source directory as the class path. 1:37 To do this after javac I will abbreviate class path with cp, 1:42 and tell it to use the source directory as our class path followed by 1:46 the full path to the Java file we'd like to compile. 1:51 Oops, looks like we got an error. 1:59 Hey, remember when we added the override annotation to 2:02 the two string method of the cheese class? 2:06 Yup, we better fix that so 2:08 that we're properly overriding the java object's toString method. 2:10 And to do this we'll simply delete the string parameter which 2:13 does not belong there. 2:17 Now let's recompile. 2:20 Cool, no errors. 2:23 We can see the .class files that were created by refreshing the override 2:25 directory. 2:28 And there they are, main.class and cheese.class. 2:32 Let's run the main class using the Java command. 2:36 Now, we might try to do this using the dot notation as follows, 2:39 java com.teamtreehouse.override.main. 2:42 But we get an error saying that the java virtual machine, 2:48 or the JVM, cannot find our main class. 2:51 This is because the JVM uses the current directory as the class path by default 2:54 when running a java program. 2:58 In our case the current directory is the workspace directory. 3:00 With this in mind, we might be tempted to add src to the beginning of 3:04 the path as follows, src.com.teamtreehouse.override.main. 3:09 But that doesn't work either. 3:16 What we'll need to do is tell the JVM to use the source directory as the class 3:19 path, so that our package names starting with com will be found. 3:23 Let's specify the class path while running like this. 3:30 Java classpath is the source directory and 3:33 now we can use teamtreehouse.override.Main. 3:38 And there's our output, string cheese. 3:43 Looks like all is well. 3:46
You need to sign up for Treehouse in order to download course files.Sign up