
A hands-on example project to run on JVM
Let's create a slightly over-engineered program that prints some JVM information to the console and consists of three classes. We will not use an IDE but a normal text editor and the Command Prompt (Windows) or Terminal screen (macOS/Linux) to compile the code. Finally, we will run the application on the command line as well. The classes in the project are put in the following packages:
- com.example.app
- com.example.app.model
- com.example.app.view
Create a root directory where you will store both the source files and the compiled files. In this directory, create both src and bin subdirectories.
In the src directory, create the following subdirectories:
- com
- com\example
- com\example\app\model
- com\example\app\view
Launch your favorite text editor, and in the model subdirectory, create a ModelFoo.java file with the following content:
package com.example.app.model;
public class ModelFoo {
public String getJVMInfo() {
return "JVM version " + System.getProperty("java.version") +
" by " + System.getProperty("java.vendor");
}
}
The ModelFoo class contains one public method that returns String with some information about the used JVM. In case you're wondering, the System class is always available in Java, as will be explained in the Java chapter. Its static getProperty() method returns the values of properties - the two used here are both built-in properties that return the JRE version and vendor, respectively.
In the view subdirectory, create the ViewBar.java file:
package com.example.app.view;
import com.example.app.model.ModelFoo;
public class ViewBar {
public void showJVMInfo(ModelFoo model) {
System.out.println("This program is running on " +
model.getJVMInfo());
}
}
This class simply prints the version information supplied by the model object to the console.
Finally, create the Controller.java file in the app subdirectory:
package com.example.app;
import com.example.app.model.ModelFoo;
import com.example.app.view.ViewBar;
public class Controller {
public static void main(String[] args) {
ViewBar view = new ViewBar();
view.showJVMInfo(new ModelFoo());
}
}
The Controller class glues the two other classes together and contains the main() method. This is not a very good example of a Model-View-Controller design pattern implementation; I cut some corners to save space.
Note that the src subdirectory structure follows the package names. This is a convention of Java; some other languages do not follow this convention for source files. JVMs always require this structure for the language's compiled files, though.
Open your operating system's Command Prompt (Windows) or Terminal Window (macOS/Linux) and change the active directory to your project's root directory (that holds the src and bin subdirectories). Compile the code by running the following command. Use your operating system's convention to specify the path to Controller.java; here, Windows' convention is used:
javac -sourcepath src -d bin .java
Quite a few things are happening here:
- The -sourcpath src option tells the compiler that all of the source code resides in the src subdirectory.
- The -d bin option tells javac to put the compiled files in the bin subdirectory. This directory must exist; javac will create its needed subdirectories automatically, though.
- Finally, the path to the main program's source file is passed.
Because the Controller.java source file imports the other two classes and the src directory structure matches all the package names, the Java compiler will be able to find all the classes and compile them.
This results in the following output directory, bin:

Let's run the application with the java command. In your command-line window, change the active directory to the bin subdirectory and run the following command:
java com.example.app.Controller
On my machine, this produces the following line:
This is running on JVM version 1.8.0_112 by Oracle Corporation