Run Your First Java App ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-16

How to Create and Run Your First Java Application. Practical Tasks

Practice tasks for the lesson on creating your first Java application. You will create classes in multiple packages, compile them with javac, run the program from the command line, pass command-line arguments to main(), build a JAR file, and add it to the classpath with the -cp flag at both compile time and run time. Each task comes with a solution on Patreon.

Create and Run the Car, Engine, and Driver Classes

  1. Create three classes: Car in the com.company.vehicles package, Engine in the com.company.details package, and Driver in the com.company.professions package. All three classes live in different packages.
  2. In the main() method of the Car class, create objects of the Engine and Driver classes. When Car runs, the program should print "I'm driving!" to the console. Compile and run the program from the command line.
  3. Use command-line arguments when running Car: the program receives several arguments and should print them in the following format:
    Value: arg1
    Value: arg2

Easy to get wrong

A class that belongs to a package is launched by its fully qualified name from the directory that contains the package root: java com.company.vehicles.Car. If you step into the vehicles directory and run java Car, the JVM will answer with Error: Could not find or load main class.

Solution on Patreon →

Build a JAR File and Add It to the Classpath with -cp

  1. Move the com.company.professions package to a separate project.
  2. Keep the object creation in the Car class: Engine engine = new Engine(); and Driver driver = new Driver(); We have not covered how object creation works yet — that comes in later lessons. What matters here is different: the Car class references the Driver class, which now lives in another project.
  3. Package com.company.professions into a JAR file and copy it to the lib directory.
  4. When compiling and running the Car class, pass the JAR file in the -cp (classpath) option.

A subtle detail

The classpath separator depends on the operating system: a semicolon on Windows (-cp ".;lib/professions.jar") and a colon on Linux and macOS (-cp ".:lib/professions.jar"). Do not drop the dot: without it, the JVM stops seeing your own classes in the current directory.

Solution on Patreon →

Check your code

While working on the tasks, keep an eye on the code style recommendations.

Frequently Asked Questions

How do I compile and run a Java program from the command line?

Compile: javac -d out src/com/company/vehicles/Car.java — the -d flag sets the directory where the .class files go, preserving the package structure. Run: java -cp out com.company.vehicles.Car — the class name is given in full, package included, without the .class extension.

How do I pass command-line arguments to a Java program?

List them after the class name, separated by spaces: java com.company.vehicles.Car red 90. They arrive in the String[] args parameter of main(): args[0] is the first argument, args.length is their count. All arguments come in as strings; convert numbers explicitly, for example with Integer.parseInt(args[1]).

How do I create a JAR file from the command line?

With the jar tool that ships with the JDK: jar cf lib/professions.jar -C out com/company/professions. The c flag creates an archive, f sets the file name, and -C out tells the tool to take classes from the out directory without including out itself in the paths inside the archive. Inspect the contents with jar tf lib/professions.jar.

What is the -cp flag and why do I need it?

-cp (short for classpath) tells the compiler and the JVM where to look for classes: directories and JAR files. Example: javac -cp lib/professions.jar ... and java -cp ".;lib/professions.jar" com.company.vehicles.Car (the separator is a semicolon on Windows and a colon on Linux/macOS). If -cp is not specified, the JVM looks for classes only in the current directory.

Comments

Please log in or register to have a possibility to add comment.