Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
Throughout this course I'll be using the Eclipse IDE to program, compile, and run my Java applications. But it's important to know that you can also compile and run from the command line and there are things you can see on the command line a little bit more easily than in the IDE. I'm going to use an existing project. I'll import the project from the Exercise Files folders using the Import command and selecting the root directory under Exercise Files>03_GettingStarted>CommandLine.
I'll complete the Import process. I'll open the application that's in the default package of the project, and this time the name of the class is Main rather than HelloWorld. You can name this Main class anything you want, the only real requirement is that the initial character be uppercase; in this case the letter M, but you can name it anything you want as long as you follow Java naming conventions. You can't use any spaces or special characters and that initial character really should be uppercase.
Once again, this is an incredibly simple application; it's outputting a string to the command line. With the application open, I'll click the Run button, the application runs, and the console displays the outputted string. Now let's go to a command window. On Windows 7 and Windows Vista, click the Start Menu and type cmd and press Return, that will open up a command window. If you're working on Mac, go to the Applications folder, to the Utility subfolder, and open the Terminal application.
In either Windows or Mac, you should start off at your Home Directory, Users\, and your username. If you're working on Windows XP, you should start off at the Documents and Settings folder and then your username. Now, change to the Desktop folder, type cd Desktop. On either Mac or Windows you can type the beginning of a folder name and then press the Tab key. So now, I'm going to move to the Exercise Files folder by typing cd and then Ex, and then press Tab.
If you're working on Windows, the case doesn't matter, but if you're working on Mac, it does; Mac is case sensitive. So now I'm in the Exercise Files folder, and I'll move down a directory into the Chapter 03 folder, typing cd 03, pressing Tab, and now I'll adding a slash. If you're working on Windows, use a backslash, and if you're working on Mac use a forward slash, and type the beginning of the project name, com, press Tab again, and then press Enter or Return and you're in the CommandLine folder.
Type dir on Windows or ls on Mac and you'll see a listing of the files in the directory. You should see two files starting with the dot; the classpath and the project files, and a folder named bin and one named src. List the contents of the src folder. Type dir on Windows or ls on Mac, then a space, and src, and you should see that it contains the one Java file, Main.java. Now switch to that directory, type cd src and press Enter or Return.
So now, I'm going to show you how to compile the application from the command line. First I'll clear the screen, type cls on Windows or clear on Mac and press Enter or Return. To compile an application use the javac command. I'll type javac, all one word, and then a space, and then the name of the Java file, including the file extension, Main.java. Regardless of whether you're working on Mac or Windows, this is case sensitive. Even though Windows is a non case sensitive file system for the most part, Java is a highly case sensitive language, so type the name of the file exactly as it exists, with the uppercase M. Then press Enter or Return.
If you don't see any output, that's good news. Now, list the contents of the directory again and you should see that the class file was created in the same directory as the source code file. Now, to run the application, type java space, and then the name of the file, but without the dot class extension, and press Enter or Return and the application should execute and the string should be output to the command line. Notice that when you execute the application, you're referring to the class as a class, not as a file.
That's why you don't include the dot class extension. When you compile, you're dealing with the file; when you run, you're dealing with the class. Now, when you compile in Eclipse, you may have noticed that you're creating the class file in a different directory, not in the same directory as the source code file. You can do the same thing from the command line. The javac command has a whole bunch of different options available. You can see those options by simply typing javac, without passing any values, such as filenames, just type javac and press Enter or Return, and you'll see a listing of all of the different options.
I'm going to use this one, -d, specifying where to place generated class files. So I'm going to clear my screen again, cls on Windows or Clear on Mac. Then I'll type javac. I'll once again follow that with the name of the file, Main.java, but then I'll type -d ..\bin, using a backslash for Windows or a forward slash for Mac. The result will be that the class file is generated in the bin folder.
I'll type cd ..\bin, I'll list the contents of the directory, and I'll see the new copy of Main.class. Now, because I've changed my current directory to bin, I can run the class from here, with java Main. Let's switch back to the source folder, cd ..\src. And I'm going to show you one incredibly valuable thing that running from the command line will let you do. I'll once again clear my screen.
Now, this time when I compile I'll use the verbose parameter, -verbose means tell the compiler to tell me what it's doing in the background. When you have issues with your Java code that you can't solve in Eclipse, sometimes seeing what the Java compiler is doing can be useful. Here is the syntax, javac, then the name of the file, Main.java, and then after a space, -verbose, all lower case. Press Enter or Return and you'll see that the class is being compiled, but you will see a whole listing of what's going on in the background.
Up at the top it says parsing started Main.java and then parsing completed. So it takes a moment first to read your file and figure out what it's supposed to do. Now it looks for supporting files, the search path for class files is called the classpath, and by default it includes all of the jar files and classes that are a part of the core Java Class Library. Those are the files that are included with the JDK. Next, it starts loading classes that it needs.
I've asked to use the String class and the System class and also the PrintStream class, which has its own set of dependencies. So the compiler goes through all of its archived files and finds the classes that it needs to build this program. Not just my class, which is called Main, but all of the supporting classes as well. Finally, it finishes the compilation and writes Main.class to the file system, and tells you how long the whole thing took; less than a second.
So that's a look at some of the things you can do working from the command line. You can do many of these things also in Eclipse, by adding parameters in your run configuration, and I will show you some of those options in the next video.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.