Join David Gassner for an in-depth discussion in this video Creating a Java project in IntelliJ IDEA, part of Java Essential Training.
- As I've shown previously, you can code in a simple text editor and compile and run your Java classes from the command line but most Java developers use integrated development environments or IDEs and I'll be using IntelliJ IDEA. In the Intellij IDEA welcome screen, I'll click Create New Project. I'll choose Java as the type of the project and I'll set my project SDK to 1.8, the SDK that I defined when I installed IntelliJ IDEA.
I'll click Next. And on this screen, I'll select the option to create a project from the template Command Line App. That'll create a Java class that has a main method just like the Java class I created previously. On this screen, I'll set the name of the project to IDEAProject and then I'll change the project location. I'll browse and I'll go to my Desktop, to my Exercise Files, to the subfolder for this video 03_04.
And then I'll append to that the name of the project, IDEAProject. So this folder will be the root folder for this project. Next, I'll set the base package. I'll use the same package that I used previously: com.example.java. When you do this through IntelliJ IDEA, you'll add this base package declaration to the Java code but you'll also create the equivalent folder hierarchy to store the class.
And then I'll click Finish to create the project. I'm told that this project file directory doesn't exist and I'll click OK to create it and create the Java code. Now if this is the first time you've done this in IntelliJ IDEA, your display won't look exactly like mine. Down at the bottom of the screen, you'll see a message saying Indexing and you'll need to wait until that's complete before you follow along with the next steps. But when the setup process is complete, you should see this display with the project window on the left and the editor on the right.
I'm going to maximize this window so we can see it more clearly. You'll also notice that I have line numbers being displayed on the left. Here's how I set that up in IntelliJ IDEA. I went to the File menu and I chose Settings on Windows or Preferences on Mac. Then I went to the Editor category, then General, to Appearance and I checked Show Line Numbers here and clicked OK. So here's exactly the same sort of code that I showed previously.
I have a class named Main and it has a main method in line 5 and any code that I want to execute should go right here where the comment says, "write your code here." I'll delete that comment and then I'll output a message. As before, I'll use System.alt.critln but I'll go through this slowly to show how IntelliJ IDEA will help you code. I'll type the beginning of the System class name: Sys and I see a list displayed of all the different classes that start with that string.
Then I can press enter or return to select it. When I press ., I see a list of elements that I can choose from the System class and this will start with a list of methods that are members of the class. But when I type out, I see that this is an object, something called a PrintStream. We'll talk about PrintStreams much later in the course but for now, I'll just select it and then press the . character again. And now I see methods that are members of the out object.
The PrintStream and I'll start typing print and then ln and I see that there are many versions of the println method, that it subs various values as arguments, booleans, characters, doubles, objects, and strings. I'm going to be using this version so I'll press the down arrow a few times and choose it and then I can type in a " character and again IDEA is helping me out by closing the quoted string with a closing double quote character and I'll say, "Hello from IDEA!" Now in IntelliJ IDEA, I don't need to explicitly save my code.
It's being saved for me as I type and I'm getting all sorts of hints and help as to how I can improve the code. Whenever you see this little lightbulb over on the left, that indicates that there might be some help available. I'll be using this feature quite a bit later on in the course but for now, I'll ignore it. To run the application, I'll go to the menu and choose Run and notice that I have two menu items that say Run, one that says Run...
and one that says Run Main. They're both enabled. That isn't always true in an IntelliJ IDEA project. If I had created a project that didn't come from a template, the first Run menu choice would have been disabled and I would've had to choose this one which creates something called a Run Configuration and when you choose that menu choice, you'll see a list of new or proposed Run Configurations. I'll choose the Main Configuration and after a moment, my Java code is compiled and executed and the Run window opens at the bottom of the screen and I see the output: Hello from IDEA! So now, I can make changes to my code and execute it again.
This time, I'll create a string variable. I'll declare its data type as String and I'll name it aString and I'll give it a value of my first name then I'll add the semicolon at the end of the line. Then I'll output a string again. This time I'll type sout and that's an abbreviation that opens a code template that will expand to system.out.println when I press enter or return.
So that's a shortcut that's available in IntelliJ IDEA and similar shortcuts are available in the other popular Java IDEs. Then, I'll enter a literal string of, "Your name is " and then I'll add a + after the string and then I'll start typing the name of the variable, aString and I see that IntelliJ IDEA knows that that variable exists and I can press enter or return to select and auto-complete it and once again, I can run my code.
Now to run the code this time, I'll open a toolbar that'll appear at the top of my workspace. I'll choose View, Toolbar and now I can run my code by clicking on the Run button on the toolbar. Then I see the output, "Hello from IDEA! Your name is David." You can run your code from the toolbar, from the menu choice, or by pressing the associated keyboard shortcut which you'll find on the menu. So now you know how to create a brand new project in IntelliJ IDEA and these are the projects that we'll be using for most of the course.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files