Java Project Overview, Compilation and Execution

Jakob Jenkov
Last update: 2015-02-16

I have explained how to create, compile and run your first Java app, and learned more about the main method. The next step in learning Java is to get an overview of how a basic Java project is structured, how the Java code is compiled, and how to execute the finished Java program.

Java Source and Class Directories

A simple Java project contains a single directory inside which all the Java source files are stored. The Java files are usually not stored directly inside the source directory, but in subdirectories matching their package structure. Packages are just a way to group together source files which belong together.

The source directory is often called src, but this is not a requirement. For instance if you use the build tool Maven then you will typically use a different directory structure, where the Java source code is stored in the src/main/java directory (under the root directory of your project).

When you compile all the source code in the source, the compiler produces one .class file for each .java file. The .class contains the compiled version of the .java file. The byte code for the .java file in other words.

It is the .class files that the Java Virtual Machine can execute. Not the .java files. Therefore it is normal to separate the .java files from the .class files. This is normally done by instructing the compiler to write the .class files into a separate directory. This directory is often called classes, but again, it is not a requirement, and it depends on what build tool or IDE etc. you are using.

Compiling the Java Source Code

You can compile your Java source directly from your IDE (if you use an IDE). Or, you can use the Java compiler that comes with the Java SDK. To compile your Java source code from the command line with the Java compiler, do this:

  • Open a command prompt
  • Change directory to your project's root directory (not the source directory)
  • Make sure the project root directory contains a source directory and a class directory
  • Type in the command below (on Windows - other OS'es will look similar though):
"c:\Program Files\Java\jdk1.8.0_25\bin\javac" src/myfirstapp/*.java -d classes

This command executes the javac command (the Java compiler) which compiles the Java sources in the directory src/myfirstapp . The *.java means the compiler should compile all files in the given directory.

The myfirstapp directory is a package under the root source directory src. If you have multiple packages under the root source directory you will have to run the Java compiler multiple times. A Java IDE handles this for you automatically. So does build tools like Ant, Maven or Gradle.

Running the Compiled Java Code

Once the compiler has done its job, the classes directory will contain the compiled .class files. The package structure (directory structure) from the source directory will be preserved under the class directory. You can run any one of these .class files which have a main() method in it. You can run the .class from inside your Java IDE, or from the command line. From the command line it looks like this:

 "c:\Program Files\Java\jdk1.8.0_25\bin\java" -cp classes myfirstapp.MyJavaApp

The -cp classes flag tells the Java Virtual Machine that all your classes are located under the directory called classes. This is also called the "Class Path" (hence the abbreviation cp).

The name of the class to run is the last argument in the above command - the myfirstapp.MyJavaApp part. The JVM needs to know the fully qualified class name (all packages plus class name) to determine where the corresponding .class file is located.

When you run the the class your command line will look similar to this (including the output from the Java app):

D:\data\projects\my-first-java-app>"c:\Program Files\Java\jdk1.8.0_25\bin\java"
    -cp classes myfirstapp.MyJavaApp
Hello World!

D:\data\projects\my-first-java-app>

(Note, the first command should not have a line break in it. I have put that in only to make it easier to read.

That is it! Now you know how to compile and execute your Java apps! As mentioned earlier, it can be easier using an IDE, but you may not have an IDE everywhere you want to execute the code.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next