Executing examples from the command line

To execute the examples from the command line, follow these steps:

  1. Go to the Learn-Java-12-Programming folder created in step 4 in the Importing a project section, where the pom.xml file is located, and run the mvn clean package command:

  1. Select the example you would like to run. For example, assuming you would like to run ControlFlow.java, run the following command:
java -cp target/learnjava-1.0-SNAPSHOT.jar:target/libs/* \
com.packt.learnjava.ch01_start.ControlFlow

You will see the following results:

  1. If you would like to run example files from the ch05_stringsIoStreams package, run the same command with a different package and class name:
java -cp target/learnjava-1.0-SNAPSHOT.jar:target/libs/* \
com.packt.learnjava.ch05_stringsIoStreams.Files

If your computer has a Windows system, use the following command as one line:

java -cp target\learnjava-1.0-SNAPSHOT.jar;target\libs\* com.packt.learnjava.ch05_stringsIoStreams.Files

Note that a Windows command has a different slash and semicolon (;) as the classpath separator.

  1. The results will be as follows:

  1. This way you can run any class that has the main() method in it. The content of the main() method will be executed.