Sub topic 10
Run the Program in an IDE
An integrated development environment, or IDE, is an app for writing and running programs. It brings tools such as a code editor and a run button into one place. An editor is the part of an app where you view and change text.
An IDE is optional. You can keep using the terminal to work with Java. An IDE can make it easier to see your files, spot mistakes, and run your program without typing commands each time.
This chapter assumes you have already installed a Java Development Kit, or JDK, and created the HelloWorld.java file. The JDK provides the tools that compile and run Java programs. You will use that same file and JDK in the IDE.
Choose an IDE
Several IDEs can work with Java. This chapter uses Visual Studio Code, also called VS Code. It runs on Windows, macOS, and Linux.
VS Code needs extra tools for Java. An extension is an add-on that gives an app more features. You will install an extension pack, which is a group of related extensions, to add Java support.
You can also use another Java IDE, such as IntelliJ IDEA or Eclipse. Its screens and menus will look different, but the basic task is the same: open your program, select a JDK, and run it.
Install VS Code and its Java extensions
If VS Code is already installed, open it and go to the extension steps below.
- Visit https://code.visualstudio.com/ and download VS Code for your operating system.
- Open the downloaded installer and follow its prompts. On macOS, you may need to move the app into your Applications folder.
- Open Visual Studio Code.
- Open the Extensions view by selecting the Extensions icon on the side of the window. It looks like a group of small squares.
- Search for Extension Pack for Java.
- Find the pack published by Microsoft and select Install.
- Wait for the extensions to finish installing. If VS Code asks you to reload the window, agree to do so.
The pack gives VS Code tools to understand, run, and debug Java programs. Debugging means examining a program while it runs to understand what it is doing.
The first time you open a Java file, the extensions may take a little time to start. If you do not see Java features right away, wait for any setup messages to finish.
Open your program folder
VS Code can open a single file, but opening the folder that contains your program makes it easier for the Java tools to find and work with that file.
- In VS Code, choose File, then Open Folder.
- Find the folder that contains HelloWorld.java.
- Select that folder and open it.
- If VS Code asks whether you trust the authors of the files, read the message. If this is your own folder and you trust its contents, choose to trust it.
- In the Explorer view on the side of the window, select HelloWorld.java.
The Explorer is the part of VS Code that lists files and folders. Your Java file should now appear in the editor.
A folder open in VS Code is often called a workspace. You do not need to create a special Java project for this small program. The folder containing your existing file is enough to follow this chapter.
Check that the file shown in the editor is your HelloWorld.java file. If you have copies in several folders, it is easy to open the wrong one. The Explorer shows which folder you opened.
Let VS Code find the JDK
VS Code is the place where you work with your program. The JDK provides the Java tools used to run it. Installing VS Code does not replace the JDK.
The Java extensions often find an installed JDK automatically. If they do, you may not need to change anything. If VS Code reports that it cannot find a JDK, or if it selects the wrong one, check its Java settings:
- Open the Command Palette. This is a box where you can search for VS Code commands. Choose View, then Command Palette, or press Ctrl+Shift+P on Windows and Linux or Command+Shift+P on macOS.
- Type Java: Configure Java Runtime and select that command.
- Look at the JDKs and project settings shown on the configuration page.
- If your installed JDK is missing, use the page’s option to add or locate it. Select the JDK’s main folder, not its bin folder.
- If several JDKs are listed, choose the one you intend to use as the default for this program, following the options on the page.
The exact buttons can change between versions of the extensions. Read the labels on your screen and follow any prompts.
The Java tools inside VS Code may have their own JDK requirements. If a message says that the Java extensions need a newer JDK, follow that message. The JDK needed by the extensions is not always the same as the one selected to run a program.
Run HelloWorld.java
Return to HelloWorld.java in the editor. Wait for the Java extensions to finish starting if they are still working.
- Save the file. Choose File, then Save, or press Ctrl+S on Windows and Linux or Command+S on macOS.
- Find the words Run and Debug near the main method in the editor. A method is a named set of instructions in a Java program.
- Select Run.
- Look for the program’s output in the lower part of the VS Code window. Depending on your settings, it may appear in the Terminal or Debug Console panel.
A panel is an area of the VS Code window that displays tools or information. If the lower panel is closed, open it through the View menu.
You should see:
Hello, World!
You may also see other text about starting the program. The greeting is the important part. When you select Run, VS Code uses the Java tools to prepare and start your program. You do not have to type the run command yourself.
If you do not see Run above the main method, make sure the file is named HelloWorld.java and that the Java extensions have finished installing and starting. Also check that VS Code has opened the folder containing the file.
Change the greeting and run again
The editor lets you change your source file and see the result. A source file is a text file containing the instructions you write for a program.
- In HelloWorld.java, find the text Hello, World! between the quotation marks.
- Change it to Hello, IDE! Leave the quotation marks and the rest of the line in place.
- Save the file.
- Select Run again.
- Look for Hello, IDE! in the output.
Saving before you run makes it clear which version of the file you are testing. If you still see the old greeting, check that you saved the file and look at the newest output. The panel may still contain text from an earlier run.
You can change the greeting back to Hello, World! and save again. Your changes are in the same HelloWorld.java file that you opened. The IDE does not create a separate copy just because you edited the file.
Try a breakpoint
A breakpoint is a marker that tells the IDE to pause a running program at a chosen line. This is useful when you want to look at what a program is doing before it finishes. Hello World is small, but it is a safe place to try the tool.
- In the editor, find the line that prints the greeting.
- Click in the narrow space just to the left of that line number. A red dot should appear. This is the breakpoint.
- Select Debug near the main method, rather than Run.
- Wait for the program to pause at the marked line.
- Look for the highlighted line and the debugging controls in VS Code.
- Select Continue. The program should print its greeting and finish.
- Click the red dot again to remove the breakpoint.
While the program is paused, the highlighted line is about to run. The greeting may not appear until you continue.
The debug view may also show variables. A variable is a named place where a program can store a value. Your small program has little to inspect, but the same view becomes useful in larger programs.
If you want to stop a paused program without continuing it, use the Stop control in the debugging controls.
Read IDE messages
VS Code can mark a line when its Java tools notice a problem. It may show a colored line under text or list a message in the Problems panel. Open that panel from the View menu.
Read the message and check the line it points to. For example, if you accidentally remove a quotation mark while changing the greeting, the editor may mark the line as having a problem. Put the missing mark back, save the file, and check whether the message goes away.
A marked line is a helpful clue, not always a complete explanation. The actual mistake may be just before the place VS Code marks.
For this chapter, you do not need to fix every kind of Java error. The aim is to recognize where the IDE shows messages and use them to guide your next step.
Resolve common IDE problems
The following checks focus on the IDE. If your JDK or program also fails outside VS Code, return to the earlier setup or troubleshooting steps.
| What you see | What to check |
|---|---|
| No Run or Debug option appears | Open HelloWorld.java, confirm the Java extension pack is installed, and wait for it to finish starting. Open the containing folder rather than a different copy of the file. |
| VS Code says it cannot find a JDK | Open Java: Configure Java Runtime and locate your installed JDK. Read any message about the JDK version needed by the extensions. |
| The program prints the old greeting | Save the file, run it again, and look at the newest output. Check that you edited the file in the folder you meant to open. |
| You cannot find the greeting | Open the lower panel and check the Terminal and Debug Console. The greeting may be among other text from the run. |
| A breakpoint does not pause the program | Make sure the red dot is beside the line that prints the greeting. Select Debug, not Run, and wait for the debugger to start. |
| The program pauses and seems stuck | It may be waiting at a breakpoint. Select Continue or Stop in the debugging controls. |
When to use an IDE
An IDE is helpful when you want to edit files, run a program, and examine messages in one window. Its tools can also help you explore a program one step at a time.
The terminal is still useful. IDEs can hide some of the commands they run, so knowing the terminal workflow helps you understand and check what is happening. You can choose the tool that suits the task.
For now, the key idea is that VS Code helps you work with Java; the JDK does the Java compilation and running. Your HelloWorld.java file remains a regular file that you can open in either workflow.
Exercises
Exercise
In VS Code, change the greeting to Welcome to Java!, save the file, and run it. What output should you look for?
Show answer
Look for Welcome to Java! in the Terminal or Debug Console panel. Then change the greeting back if you wish.
Exercise
You selected Run, but your breakpoint did not pause the program. What should you try?
Show answer
Select Debug instead of Run. Make sure the breakpoint is beside the line that prints the greeting.
Exercise
VS Code reports that it cannot find a JDK. Where can you check which JDK it is using?
Show answer
Open the Command Palette and select Java: Configure Java Runtime. Check the listed JDKs and add or select your installed JDK if needed.