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
- Create three classes:
Carin thecom.company.vehiclespackage,Enginein thecom.company.detailspackage, andDriverin thecom.company.professionspackage. All three classes live in different packages. - In the
main()method of theCarclass, create objects of theEngineandDriverclasses. WhenCarruns, the program should print "I'm driving!" to the console. Compile and run the program from the command line. - 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.
Build a JAR File and Add It to the Classpath with -cp
- Move the
com.company.professionspackage to a separate project. - Keep the object creation in the
Carclass:Engine engine = new Engine();andDriver driver = new Driver();We have not covered how object creation works yet — that comes in later lessons. What matters here is different: theCarclass references theDriverclass, which now lives in another project. - Package
com.company.professionsinto a JAR file and copy it to thelibdirectory. - When compiling and running the
Carclass, 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.
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