Installing, Configuring, and Running Your First Java Program

UPI Study › Courses › Data Structures and Algorithms › Free lessons › Lesson 1.1 Free with a free account

Installing, Configuring, and Running Your First Java Program

Data Structures and Algorithms · Chapter 1: Getting Started with JavaSection 1.1 · lesson 1 of 8712 min · free with a free account

Next lesson: How Java Reads and Executes Instructions Step by Step →

Earn 3 college credits for Data Structures and AlgorithmsRecommended for credit by NCCRS · Computer Science 251 · self-paced · one payment, lifetime access

In this lesson

Install the JDK, then compile and run a first Java program.

  • Install a JDK of Java 21 or newer and verify the java and javac versions
  • Explain the roles of javac and java and fix PATH and JAVA_HOME problems
  • Compile a source file into a class file and run it by program name
  • Describe the main method header, the print methods and basic program anatomy

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,665 words). Read along, or search it.

Opening Question

From Typed Text to Printed Message

You type a few lines of text into a file. A moment later, your computer follows them, and prints a message. What has to happen in between? Two tools do the work. One translates your text, and the other starts the translated program. By the end of this video, you'll have both installed, checked, and used on a real program.

Key Tools

Inside the Java Development Kit

Both tools come in one download, the Java Development Kit, or JDK. Think of it as a workshop. You write a recipe, which is your program. One tool checks and translates the recipe, and another carries it out, step by step. The translator is javac, the compiler. It turns a source file, ending in dot java, into a compiled file ending in dot class. The launcher, java, starts a program that has already been compiled. Those two are all you need today. The kit holds more. jshell is a playground that runs one line at a time, and jar bundles many compiled files into one archive. It also ships the standard libraries: ready-made code that your programs can use.

Choosing a Version

Install Java 21 or Newer

Which version? Java ships a new release every six months, and every two years, one of them becomes a long-term support release. Vendors keep maintaining those for years. Java 21 and Java 25 are both long-term support releases. Install Java 21 or newer, and every program in this course will work. Free builds come from several vendors, such as Eclipse Temurin and Oracle. They all contain the same standard tools, so pick any one. Download the installer for your system, and run it. On Windows, keep the options that add Java to the path and set JAVA_HOME switched on. Or use one package-manager command: winget on Windows, Homebrew on a Mac, apt on Ubuntu. If a package name has changed, use the installer instead.

Setup Step

Opening a Terminal Window

Then open a terminal: a window where you type commands and read the replies. That's Command Prompt or PowerShell on Windows, and Terminal on a Mac or Linux. Open a new one after installing, so it picks up the changes. Now ask each tool for its version.

Verification

Checking Both Version Numbers

java -version answers with the runtime's version. And javac -version answers with the compiler's. Your numbers will differ from these. What matters is that both commands answer, which proves the runtime and the compiler are both installed.

Troubleshooting

Fixing PATH and JAVA_HOME

If the terminal says javac isn't found, it doesn't know where Java lives. A terminal finds commands by searching a list of folders, called the PATH. The installer normally adds the JDK's bin folder to that list. JAVA_HOME is a second setting. It stores where the JDK is, and many tools read it. On Windows, search for environment variables, open Edit the system environment variables, and click Environment Variables. Under User variables, add JAVA_HOME, set to the JDK's installation folder. Then edit Path, and add percent JAVA_HOME percent, backslash bin. Click OK on every window, and open a new terminal. On a Mac or Linux, add export lines to your shell's settings file, dot zshrc or dot bashrc, in your home folder. On a Mac, a small helper finds the JDK for version 21. On Linux, the folder name can differ, so look inside slash user, lib, JVM. Either way, the last line puts JAVA_HOME's bin folder at the front of the PATH. Open a new terminal, and repeat both version checks.

Tool Choice

IDE or Command Line

Next, where will you write code? An IDE, an integrated development environment, combines an editor, a Run button, and helpful hints in one window. The alternative is a terminal plus any text editor. Every step is visible, and the same commands work on every computer. IntelliJ IDEA gives smart suggestions and one-click runs, with many menus to learn. Eclipse is free, with a long track record in teaching and industry. Visual Studio Code is a light editor that handles other languages too, once you add Microsoft's Extension Pack for Java. Start on the command line. An IDE's Run button simply runs javac and java for you, and an IDE pays off once programs grow to several files.

Project Structure

Keeping Source and Output Apart

A project is just a folder that keeps one piece of work together. This course uses one project folder, holding two more. src holds the source files you write. out holds the files the compiler produces. So what you wrote never mixes with what the computer made. Source files end in dot java. Each file holds one public type, and the file's name must equal that type's name exactly, capital letters included. Even on Windows and macOS, where file names usually ignore capitals, Java compares them letter for letter.

Naming Rules

Legal Names and Upper Camel Case

Names follow a few rules. They may use letters, digits, underscores and the dollar sign, but they can't start with a digit. No spaces, no hyphens, and no reserved words, such as public, which already mean something in Java. By convention, a type name starts with a capital, and every new word is capitalized, as in BankAccount. That's called upper camel case. So HelloWorld and BankAccount2 are fine. helloWorld is legal, but breaks the convention. Hello-World, 2ndProgram and public are refused.

Quick Check

Which Type Name Java Accepts

Quick check. A new program needs a type name, and three are suggested. Which one will Java accept as a name? Pause, and decide. It's Order, underscore, Form 2. Underscores are allowed, and so are digits, as long as one isn't first. Order Form has a space, which splits it into two names. And 2ndOrder starts with a digit, which is never allowed.

Hands-On Setup

Making the Project Folders

Time to build it. In the terminal, mkdir makes a new folder, and cd moves into it. These three lines leave you inside dsa-course, with an empty src folder.

Worked Example

Compiling and Running HelloWorld

In your editor, type the program, and save it as HelloWorld dot java, inside src. Now compile: javac, dash d out, then the path to the source file. Dash d out tells the compiler where to put what it makes. When it succeeds, javac prints nothing at all. Listing the out folder shows the new file, HelloWorld dot class. Run it with java, dash c p out, which says where the compiled files are, then the program's name. And there's the output.

Key Rule

Compile a File, Run a Name

One rule keeps the two commands straight: compile a file, run a name. javac needs the file's path, with its dot java ending. java needs only the program's name: no dot java, and no dot class. Add dot class anyway, and the launcher looks for a program with that whole name. It reports that it could not find or load the main class. For a single file, there's a shortcut. java, then the source path, compiles in memory and runs in one step. This course uses the two-step form, so that each stage stays visible.

Program Entry Point

Reading the Main Method Header

Now, the header on line two. When a program runs, Java needs to know where to begin, so it always looks for a method called main, with exactly this header. A method is a named block of instructions. public means it's visible from outside, so the launcher is allowed to call it. static means it belongs to the type itself, so it can run before any object is created. void means it gives back no result. main is the exact name the launcher looks for. Any other spelling is not a starting point. String, square brackets, args, is a list of text values typed after the program's name. It's often empty, but the header must include it. Newer Java versions also accept a shorter form for beginners. This header works on every version, and it's what nearly all existing code uses.

Output Methods

Println, Print, Printf and Escapes

Programs show results by printing text, through System dot out. println prints its text, then moves to a new line. print prints, and stays on the same line. So Loading and the three dots land together, and an empty println ends that line. printf prints formatted text. Percent d is replaced by a whole number, here 3, and percent n starts a new line. Inside quotation marks, a backslash starts an escape sequence: a short code for a character that's hard to type. Backslash t is a tab, a wide gap. Backslash quote prints a quotation mark. Backslash n would start a new line, and two backslashes print one. Compile and run it, and match each line of output with its statement.

Quick Check

Counting Lines of Mixed Output

Quick check, with a new program that mixes print and println. How many lines does it print, and what is on each one? Pause and predict. It prints two lines. The two print calls stay on one line, and println Structures finishes that line. Then Ready gets a line of its own. The one-line answer forgets that println ends its line, and the three-line answer treats print like println.

Program Anatomy

Statements, Braces and Comments

Last, the anatomy of any Java program. A statement is one complete instruction, and it ends with a semicolon, the way a sentence ends with a period. Braces group instructions into a block, and every opening brace needs a closing one. Here, the class's block holds the main method's block. A comment starts with two slashes. It's written for people, and the compiler skips it. Java ignores extra spaces and blank lines. But indenting four spaces for each level of braces shows, at a glance, which lines belong to which block. So which lines end with a semicolon? Statements do. A line that opens a block ends with a brace instead, and closing braces and comments need nothing.

Key Takeaways

Toolbox, Names and Starting Point

To sum up. The JDK is the toolbox: javac compiles, and java runs. Install Java 21 or newer, and check both versions. A file's name must match the public type inside it. Every program starts at public static void main. Compile a file path, then run a program name. Statements end with semicolons, and braces group them into blocks.

Think About It

Standardizing a Team's Java Setup

Something to think about. A team of ten each installs a different Java version, uses a different editor, and stores files their own way. Which parts would you standardize, so that everyone's programs compile and run the same way? And why?

About Data Structures and Algorithms at UPI Study

Data Structures and Algorithms (Computer Science 251) is a self-paced online course of 87 video lessons in 17 chapters, from Java and object-oriented design to arrays, linked lists, recursion, stacks and queues, trees, heaps, hash tables, sorting and graphs. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Discrete Math vs Data Structures and Algorithms · Intro to Java vs Data Structures and Algorithms

Why take it here

Why students take Data Structures and Algorithms with UPI Study

How programs store and search dataData Structures and Algorithms teaches how to organize data and measure how fast code runs: arrays, linked lists, stacks, queues, trees, heaps, hash tables, sorting and graphs, all written in Java. It is a core course in computer science and software degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Data Structures and Algorithms carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Installing, Configuring, and Running Your First Java Program

What is the difference between javac and java?
javac is the Java compiler, and java is the launcher that runs compiled programs. javac checks a source file ending in .java and translates it into a .class file. The java command then starts a program that has already been compiled. A simple way to remember it is compile a file, run a name: javac -d out src/HelloWorld.java, then java -cp out HelloWorld.
What does public static void main(String[] args) mean in Java?
It is the standard header of the method where a Java program starts. public lets the launcher call it, static means it belongs to the class so it can run before any object exists, void means it returns no result, and main is the exact name the launcher looks for. String[] args holds any text values typed after the program's name, and it is often empty.
What is the difference between print and println in Java?
System.out.println prints its text and then moves to a new line, while System.out.print prints and stays on the same line. So System.out.print("Data "); followed by System.out.println("Structures"); prints Data Structures on one line. System.out.printf prints formatted text: in printf("%d%n", 3), %d is replaced by the whole number 3 and %n starts a new line.
Why does Java say could not find or load main class?
This error means the java launcher could not find a compiled class with the exact name it was given in the folders on its class path. A common cause is typing java -cp out HelloWorld.class, which makes the launcher look for a class with that whole name. Run java -cp out HelloWorld instead, with no .java or .class ending, and make sure -cp points to the folder holding HelloWorld.class.

About the Data Structures and Algorithms course

How many college credits is Data Structures and Algorithms?
Data Structures and Algorithms (Computer Science 251) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Data Structures and Algorithms cost and what is included?
$250, paid once, with lifetime access: all 87 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Data Structures and Algorithms take?
It is self-paced with no deadlines. The 87 lessons total about 15 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Data Structures and Algorithms.

87 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Data Structures and Algorithms
3 credits · NCCRS
Enroll →