Java command line programs use a main method in order to run code when the user launches the program. These main methods often accept parameters so the user can specify certain options that will be used when the program runs. This video will show how to create a main method and read the options that have been given to you by a user.
- [Narrator] In this course, we're gonna use Eclipse to create, test, and run our applications. This will not be an attempt to tell you everything about Eclipse and how to use it. In fact, we'll barely be scratching the surface. Eclipse is an incredibly powerful tool with a huge number of features and add-ons. If you do want to learn more about how to use Eclipse, and I really recommend that you do, please check out our other courses that deal specifically with Eclipse, like Eclipse Essential Training and Up and Running with Eclipse. That being said, you'll be learning several useful things about Eclipse throughout this course to get you started.
In the first section, we'll create a command-line application you can use as a simple way of writing and running your code. You might be thinking, "A command-line application? "What is this, 1972? "Why are we even talking about this?" Well, there are still some good uses for command-line applications. First of all, they're an excellent first step when you're learning to use an IDE like Eclipse. You don't get distracted by all the options and setup of your coding environment. You can just jump right in there and start writing code. Second, command-line apps are great for quick tests of your code.
Instead of launching the full interface required by your application and maybe having to go through several screens or graphical prompts and button clicks, you can directly run the specific bits of code that you're working on and just see what happens. And third, people still use command-line applications. Admins still write scripts to manage their servers and users. Scheduled background tasks still need some way of launching applications or running code that has not graphicaL interface at all. And frankly, some things you need to do are just so simple that writing a full GUI just to run the code is total overkill.
Sometimes simple things need simple solutions. So, let's get started writing our first application in Eclipse. At the opening screen of a new Eclipse project, you see a few links to things that help you get started. This looks different on different versions of Eclipse. But they all have this gold arrow icon that brings you to the Workbench screen. I like to start with the Workbench screen because it feels more like a programming environment. From the Workbench, we can create a new project. Click the first icon in the toolbar to bring up the Project Wizard. You can also get there using the Menu option, File, New Project.
Then expand the Java entry in the tree so you can choose the Java Project option. You can either double click this or select it and click the Next button. On the Create a Java Project screen, just give the project a name. Here I'll call it FileSearch. There are other things we can configure here if we want, but the default options will work just fine. Click the Finish button. I'm going to add a little screen real estate here by closing the Task List and the Outline views on the right hand side.
Those are useful as your program gets larger and more complex, but right now they're just taking up space. If you want them back, you can open them individually from the Window menu. Or you can choose Window, Perspectives, Reset Perspective and reopen all the default views in their default layout. Let's create a class for our Java code. Right click the source folder. That's where our source code lives and choose the menu option, New, Class. In the new Java Class dialogue, we enter a Package name of com.example.filesearch.
And we'll use a class name of FileSearchApp because this is a command-line application, we can also click the check box next to public static void main. But otherwise, we can leave everything else as is. Click the Finish button and the new class is open in the editor window, ready for code. Since we started this section by talking about command-line applications, we can close with a little information about how Java programs run as command-line applications. That method in our Java class called public static void main is a special method with a very specific purpose.
When you run a Java class of the command-line, the Java Runtime looks for a main method in your class and it runs the code inside that method. This is exactly the same as the main function in a C or C++ application. The String args parameter is an array of strings representing all the command-line options that were passed through the application at Runtime. So, let's say our main method looks like this. I will add a for block, and I'm gonna hit ctrl + space that will give me some options for what types of for blocks I can create.
I'll create a foreach block to iterate through the String arguments. Change the variable to arg. And then I will just print out the arguments on the command line. If I type sysout in Eclipse and then I press ctrl + space, it will expand sysout to system.out.println, that will write things to the console. Now I can write each arg to the console and see what happens. We can run this at the command line with Java com.example.filesearch.FileSearchApp and then we can add whatever kind of command-line parameters we'd like.
Here I just typed one, two, three. If we press enter to run this to the command line it will print out the command-line parameters that we typed. So, now we have a command-line app that can accept arguments and run code. In the next section, we'll give it some structure.
- Creating a command-line application
- Building a graphical user interface
- Creating a Java API
- Parsing JSON data
- Refactoring and testing Java code