New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Java Essential Training
Illustration by

Compiling and running from the command line


From:

Java Essential Training

with David Gassner

Video: Compiling and running from the command line

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.
Expand all | Collapse all
  1. 10m 8s
    1. Welcome
      1m 3s
    2. Is this course for you?
      5m 35s
    3. Using the exercise files
      3m 30s
  2. 31m 24s
    1. The history of Java
      5m 19s
    2. Java compilation and syntax
      8m 54s
    3. Understanding the principles of Java
      8m 28s
    4. Choosing a development environment
      8m 43s
  3. 19m 5s
    1. Installing Java on Windows
      6m 42s
    2. Installing Eclipse on Windows
      3m 19s
    3. Exploring Java on Mac OS X Leopard and Snow Leopard
      2m 27s
    4. Installing Java on Mac OS X Lion
      3m 27s
    5. Installing Eclipse on Mac OS X
      3m 10s
  4. 46m 10s
    1. Creating a Hello World application
      11m 7s
    2. Exploring the Eclipse IDE
      8m 55s
    3. Compiling and running from the command line
      8m 2s
    4. Passing arguments to the application
      8m 17s
    5. Using the Java API documentation
      4m 5s
    6. Memory management and garbage collection
      5m 44s
  5. 58m 57s
    1. Everything is an object
      5m 59s
    2. Declaring and initializing variables
      9m 15s
    3. Working with numbers
      8m 32s
    4. Converting numeric values
      6m 40s
    5. Understanding operators
      7m 58s
    6. Working with character values
      5m 14s
    7. Working with boolean values
      5m 13s
    8. Outputting primitive values as strings
      5m 33s
    9. Creating a simple calculator application
      4m 33s
  6. 53m 40s
    1. Writing conditional code
      5m 35s
    2. Using the switch statement
      8m 50s
    3. Repeating code blocks with loops
      7m 35s
    4. Creating reusable code with methods
      6m 31s
    5. Declaring methods with arguments
      5m 41s
    6. Overloading method names with different signatures
      5m 53s
    7. Passing arguments by reference or by value
      5m 35s
    8. Creating a more complex calculator application
      8m 0s
  7. 20m 30s
    1. Using the String class
      5m 44s
    2. Building strings with StringBuilder
      3m 34s
    3. Parsing string values
      3m 19s
    4. Working with date values
      7m 53s
  8. 20m 44s
    1. Understanding compile-time vs. runtime errors
      4m 5s
    2. Handling exceptions with try/catch
      4m 55s
    3. Throwing exceptions in methods
      2m 50s
    4. Using the debugger
      8m 54s
  9. 32m 22s
    1. Using simple arrays
      4m 47s
    2. Using two-dimensional arrays
      6m 17s
    3. Managing resizable arrays with ArrayList
      7m 14s
    4. Managing unordered data with HashMap
      6m 5s
    5. Looping through collections with iterators
      7m 59s
  10. 52m 2s
    1. Understanding encapsulation
      5m 59s
    2. Creating and instantiating custom classes
      8m 8s
    3. Organizing classes with packages
      6m 47s
    4. Creating and using instance methods
      6m 52s
    5. Storing data in instance variables
      6m 56s
    6. Using constructor methods
      5m 40s
    7. Managing instance data with getter and setter methods
      8m 26s
    8. Using class variables and Enum classes
      3m 14s
  11. 41m 15s
    1. Understanding inheritance and polymorphism
      9m 12s
    2. Extending custom classes
      9m 1s
    3. Overriding superclass methods
      3m 8s
    4. Casting subclass objects
      5m 3s
    5. Understanding interfaces and implementing classes
      4m 2s
    6. Creating your own interfaces
      4m 14s
    7. Using abstract classes and methods
      6m 35s
  12. 32m 17s
    1. Managing files with the core class library
      7m 46s
    2. Managing files with Apache Commons FileUtils
      7m 32s
    3. Reading a text file from a networked resource
      7m 52s
    4. Parsing an XML file with DOM
      9m 7s
  13. 17m 39s
    1. Creating your own JAR files
      4m 54s
    2. Understanding the classpath
      5m 2s
    3. Documenting code with Javadoc
      7m 43s
  14. 47s
    1. Goodbye
      47s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Java Essential Training
7h 17m Beginner Dec 14, 2011

Viewers: in countries Watching now:

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.

Topics include:
  • Understanding the history and principles of Java
  • Installing Eclipse and Java
  • Compiling and running from the command line
  • Managing memory and performing garbage collection
  • Declaring and initializing variables
  • Writing conditional code
  • Building and parsing strings
  • Debugging and exception handling
  • Using simple arrays
  • Creating custom classes
  • Working with encapsulation, inheritance, and polymorphism
  • Managing files
  • Documenting code with Javadocs
Subjects:
Developer Programming Languages
Software:
Android Java Eclipse
Author:
David Gassner

Compiling and running from the command line

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.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.