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.
Once you've packaged up an application as a JAR File you're ready to run it from the command line, but in order to run it from the command line, you need to understand how the concept of the class path works. I'll described how to use the class path and how to set it on both Mac and Windows in this exercise. I'm working in the class path project, a version of my olivepress application that I've already compressed into a JAR File. If you have access to the Exercise Files you can use this file or you can use any other JAR File that you want to create.
I'm going to copy this file to my desktop, so I don't have to worry about the specific folder in which it's located, once I go to the command line. I'll right-click on the file and copy, then I'll go to the desktop and paste. Now, I'll open up a command window, on Windows, I'll run the cmd application and on Mac, open terminal, and then switch to your desktop folder, do a directory listing, type dir on Windows or LS on Mac, and you'll see the OlivePressApp.jar file there.
Now, to run the application you need to know the complete path of the Main class. I'll go back to Eclipse briefly, and show you within Main.java that the package is com.lynda.olivepress, and the class is main. In order to run the class from the command line you need to know both of those bits of information, you'll put the package and the class name together separated with a dot, and that's how you launch the application using the Java tool. I'll go back to the command window and I'll try running the application the first time.
I'll type java com.lynda.olivepress.Main, and I get the error could not find or load Main class and then the full path of the class that I typed. Now I typed that correctly, but the problem is that Java doesn't know where to find the class, here's how you tell it. You tell Java where the class is that you want to run by setting the class path. The class path is a list of file locations. Typically it starts with a single dot, meaning the current directory, and then lists all of the folders and JAR Files that contain compiled classes.
You can set the class path either as part of the Java command or as an environment variable and I'll show you both. Here's how you incorporate the class path into the command that starts the application, start with Java and then - classpath, you can shorten that to -cp if you prefer, then type in a dot for the current directory. On Windows, add a semicolon on Mac or Linux add a colon, and then type in the name of your JAR File.
I'll start by typing olive and then I'll tab, and on either Mac or Windows, that should auto complete the class path. Now type in the full path of your starting class com.lynda.olivepress.Main, make sure you spell it exactly correctly, it is case-sensitive, regardless of whether you're working on Windows or Mac press Enter or Return and the application should run. Now you can also set the class path as an environment variable, this works pretty much the same way on either Windows or Mac, but again the difference will be how you separate the values.
You use a semicolon to separate the values on Windows and colon on Mac. This time I'll type SETCLASSPATH put it in all uppercase, it doesn't matter on Windows, but it does on Mac, and then an equals operator, and then just as I did before I'll type in a dot, then a ; on Windows or a : on Mac, and then the name of the file, and once again, I can type the beginning of the file name and press tab. I'll press Enter or Return and then this time I won't need to add the -class path argument to the command, because the value was already set in the environment variable.
I'll type in java com.lynda.olivepress. Main, and now my application works again. When you set an environment variable this way, you're setting it only for the current command window session. For command on Windows or terminal on Mac, and when you close the command window or you close terminal, that setting will be lost. You could set this permanently in what Windows calls the system environment variables, or through a special file on Mac that I don't recommend doing it.
It's better to create a batch file or a bash script to set these values if it's something you need to do over and over again, but again, you can set the class path either as part of the Java command, or as an environment variable, whichever works best for you. Once you know how the class path works, you can get started on your own Java applications, running them either from within Eclipse as we've been doing throughout the entire course, or in the command window, if that works better for your debugging purposes.
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.