Sub topic 10
Run the Program in an IDE
An integrated development environment (IDE) is a tool that brings several programming tasks together. You can edit, run, and check your code in one place.
An IDE is optional. You can still use a terminal to compile and run Java programs. An IDE can make the work easier by showing errors as you type and offering suggestions.
In this chapter, you will use Visual Studio Code, often called VS Code, with Java tools. You should already have a Java Development Kit (JDK) installed and a file named HelloWorld.java saved on your computer.
Prepare VS Code for Java
VS Code is a code editor. You can add an extension, which is extra software that gives an editor new features. The Java extensions let VS Code act like an IDE for Java.
- Download and install VS Code from [code.visualstudio.com](https://code.visualstudio.com/).
- Open VS Code.
- Open the Extensions view by clicking the square-shaped Extensions icon on the left.
- Search for
Extension Pack for Java. - Select the pack published by Microsoft and click Install.
The extension pack contains several tools. They help VS Code understand Java files and run Java programs.
Open your program
A folder holds files on your computer. Open the folder that contains your existing HelloWorld.java file.
- In VS Code, choose File, then Open Folder. On macOS, choose File, then Open.
- Select the folder containing
HelloWorld.java. - If VS Code asks whether you trust the folder, trust it only if you created the files or otherwise know they are safe.
- In the file list on the left, click
HelloWorld.java.
You should see the program you wrote earlier. Keep the file name as HelloWorld.java: it needs to match the public class name, HelloWorld.
Run Hello World
Find the line containing main. A method is a named set of instructions in a Java program. The main method is where this program starts.
After the Java extensions finish loading, you should see Run and Debug above the main method. Debugging means looking for the cause of a problem in a program.
- Click Run.
- If VS Code asks you to choose how to run the file, choose Java.
- Look for the program’s output, the text it prints, in the terminal panel at the bottom of VS Code.
You should see:
Hello, World!
VS Code uses the JDK tools to prepare and run the program. You do not have to type the compile and run commands yourself.
Change the message and run again
Let’s use the editor to make a small change. In HelloWorld.java, find the line that prints the greeting. Change it to:
System.out.println("Hello from my IDE!");
Save the file with File > Save. Then click Run above main again. The new output should include:
Hello from my IDE!
If you still see the old greeting, check that you saved the file and ran the program again.
If the Run link does not appear
The Java tools may need a moment to load the first time you open the folder. If the link still does not appear, check the following:
- Make sure you opened the folder containing
HelloWorld.java, not just a different folder. - Check that the Microsoft Extension Pack for Java is installed and enabled.
- Check that the file still has a
mainmethod. - Open the command palette, a search box for VS Code commands, with Ctrl+Shift+P on Windows or Linux, or Cmd+Shift+P on macOS. Search for Java: Configure Java Runtime to see which JDKs VS Code can find.
The Java extension may ask for a newer JDK for its own tools. If it does, follow the extension’s instructions. That JDK can be different from the one selected for your program.
Exercise
Change the greeting in HelloWorld.java to print Hello, learner! Save the file and run it in VS Code. What text should appear in the output?
Show answer
Hello, learner!