Introduction
Java is a popular programming language used for a wide range of applications, from desktop software to mobile apps and web services. In this tutorial, you will learn how to create a new Java file, compile it, and run the resulting program using the command line. These fundamental skills form the foundation of Java development, regardless of which tools or frameworks you might use in the future.
By the end of this tutorial, you will understand the basic workflow of Java development and be able to create and execute simple Java programs from the terminal.
Creating Your First Java File
In this step, you will create your first Java file using the WebIDE interface. Java files have the .java extension and contain the source code for your Java programs.
Setting Up Your Project Directory
First, let's create a directory structure for your Java project. Open the terminal and follow these commands:
Make sure you are in the project directory:
cd ~/projectCreate a new directory for your Java project:
mkdir -p java-hello-world/srcNavigate to the source directory:
cd java-hello-world/src
The terminal should now show that you are in the ~/project/java-hello-world/src directory.
Creating the Java File
Now, let's create a Java file named HelloWorld.java using the WebIDE:
In the file explorer panel on the left side of the WebIDE, navigate to the
project/java-hello-world/srcfolder.Right-click on the
srcfolder and select "New File".Name the file
HelloWorld.javaand press Enter.The file will open in the editor. Add the following Java code to the file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Save the file by pressing Ctrl+S or by going to File > Save.
Understanding the Java File You Created
Let's take a moment to understand the code you just wrote:
public class HelloWorld { ... }: This defines a class namedHelloWorld. In Java, each public class must be stored in a file with the same name (case-sensitive).public static void main(String[] args) { ... }: This is the main method, which serves as the entry point for your Java application. The Java Virtual Machine (JVM) starts execution from this method.System.out.println("Hello, World!");: This line outputs the text "Hello, World!" to the console.
Congratulations, you have created your first Java file. In the next step, you will learn how to compile this file.
Examining the Java File Structure
Before compiling your Java file, let's take a closer look at how Java files are structured and organized.
Java File Organization
Java files are organized into a structure that reflects the logical organization of your code. The key components include:
- Package Declaration (optional): Packages are a way to organize related classes.
- Import Statements (optional): These allow you to use classes from other packages.
- Class Definition: The main content of your file, which defines a Java class.
For larger projects, the package declaration is important as it helps organize code. Here's how you could modify your HelloWorld.java file to include a package declaration:
Open
HelloWorld.javain the editor if it's not already open.Update the file to include a package declaration at the top:
package com.example.hello;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Save the file (Ctrl+S).
Moving the File to Match Package Structure
In Java, the file structure should match the package structure. Let's organize your file accordingly:
Create the directory structure to match the package:
cd ~/project/java-hello-world/src mkdir -p com/example/helloMove your Java file to the new location:
mv HelloWorld.java com/example/hello/Verify the file has been moved:
ls -l com/example/hello/
You should see output similar to:
total 4
-rw-r--r-- 1 labex labex 135 ... HelloWorld.java
Now your file structure matches the package declaration, which is a best practice in Java development.
Understanding the Relationship Between Files and Packages
The relationship between Java files and packages can be visualized as follows:
- Package:
com.example.hello - Directory structure:
com/example/hello/ - File:
HelloWorld.javainside the directory structure
This organization helps manage code in larger projects and prevents naming conflicts between classes.
Compiling Your Java Program
After creating your Java file, the next step is to compile it. Compilation is the process of converting human-readable Java code into bytecode that the Java Virtual Machine (JVM) can execute.
The Java Compilation Process
When you compile a Java program:
- The Java compiler (
javac) reads your.javafile - It checks for syntax errors
- If no errors are found, it generates a
.classfile containing bytecode
Compiling Your HelloWorld Program
Now, let's compile the Java file you created:
Navigate to the source directory:
cd ~/project/java-hello-world/srcRun the Java compiler with your file:
javac com/example/hello/HelloWorld.java
If there are no errors in your code, the command will complete without any output. This means the compilation was successful.
- Verify that a
.classfile was created:ls -l com/example/hello/
You should see output similar to:
total 8
-rw-r--r-- 1 labex labex 416 ... HelloWorld.class
-rw-r--r-- 1 labex labex 135 ... HelloWorld.java
The HelloWorld.class file is the compiled bytecode that can be executed by the JVM.
Handling Compilation Errors
If you made a mistake in your code, the compiler would display error messages. For example, if you forgot to include a semicolon at the end of a statement, you would see an error similar to:
com/example/hello/HelloWorld.java:5: error: ';' expected
System.out.println("Hello, World!")
^
1 error
If you encounter any errors, go back to your Java file, fix the issues, and try compiling again.
Running Your Java Program
After successfully compiling your Java program, the final step is to run it. Java programs are executed by the Java Virtual Machine (JVM), which interprets the bytecode in your .class file.
Understanding the Java Runtime Environment
The Java Runtime Environment (JRE) includes the JVM and provides everything needed to run compiled Java programs. When you run a Java program:
- The JVM loads the
.classfile - It verifies the bytecode
- It executes the bytecode, starting from the
mainmethod
Running Your HelloWorld Program
To run your compiled Java program:
Make sure you are still in the source directory:
cd ~/project/java-hello-world/srcUse the
javacommand to run your program:java com.example.hello.HelloWorld
You should see the following output:
Hello, World!
This confirms that your Java program ran successfully. The text "Hello, World!" was printed to the console, just as you specified in your code.
Important Notes About Running Java Programs
When running Java programs, keep in mind:
You must be in a directory where the compiler can find your package structure (in this case, the
srcdirectory).The
javacommand takes the fully qualified class name (including the package), not the file name.Do not include the
.classextension when running the program.
Making a Change and Running Again
Let's modify your program to print a different message:
Open
com/example/hello/HelloWorld.javain the editor.Change the message in the
printlnstatement:package com.example.hello; public class HelloWorld { public static void main(String[] args) { System.out.println("Java programming is fun!"); } }Save the file.
Recompile the program:
javac com/example/hello/HelloWorld.javaRun the program again:
java com.example.hello.HelloWorld
You should now see the new message:
Java programming is fun!
Congratulations! You have successfully created, compiled, and run a Java program. This workflow forms the foundation of Java development.
Summary
In this tutorial, you have learned the fundamental workflow for Java development:
- Creating a Java file with the
.javaextension - Organizing Java files into package structures
- Compiling Java code using the
javaccommand - Running compiled Java programs using the
javacommand
These skills provide a solid foundation for Java development. While modern development often involves integrated development environments (IDEs) that automate many of these steps, understanding the underlying processes is crucial for becoming a proficient Java developer.
Now that you know how to create, compile, and run Java programs from the terminal, you can start exploring more complex Java features and building more sophisticated applications.



