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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Data Structures and Algorithms.
Lesson complete
Earn the 3 college credits for Data Structures and Algorithms
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: How Java Reads and Executes Instructions Step by Step →Next lesson: How Java Reads and Executes Instructions Step by Step →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
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
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What does public static void main(String[] args) mean in Java?
What is the difference between print and println in Java?
Why does Java say could not find or load main class?
About the Data Structures and Algorithms course
How many college credits is Data Structures and Algorithms?
How much does Data Structures and Algorithms cost and what is included?
Which universities accept UPI Study credits?
How long does Data Structures and Algorithms take?
How are the exams done?
How do my credits reach my college?
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.
3 credits · NCCRSEnroll →