Run a project consisting of separate class files

When the project is stored in directories that contain class files (even when the project uses JAR files for its dependencies), the java command is normally invoked with a command like this:

java -cp "CLASSPATH" MAINCLASS ARGUMENTS

Substitute CLASSPATH with the used class path and MAINCLASS with the fully qualified class name that contains the static main() method. If the class supports arguments, they can be specified by substituting ARGUMENTS with the required arguments.

Let's have a look at a real-world example. This is a slightly simplified example, taken from a Windows batch script that is supplied with Oracle JDK's JavaDB component. It starts the Apache Derby Network Server when launched from the db subdirectory of the JDK's installation directory:

java -cp "lib\derby.jar;lib\derbynet.jar;lib\derbyclient.jar;lib\derbytools.jar;lib\derbyoptionaltools.jar" org.apache.derby.drda.NetworkServerControl start

It clearly demonstrates that all the required JAR files are specified one by one on the class path, as the JVM instance will never try to load a JAR file that was not put on the class path explicitly. The fully qualified class name that has the static void main() function is org.apache.derby.drda.NetworkServerControl and the passed command-line argument to the main function is start. Since each class path entry specified a specific JAR file and not just a directory, the mentioned class must have been placed inside one of the specified JAR files.

In modern versions of the JRE, it is possible to specify wildcards. This will also load JAR files that can be matched. Preceding example can be simplified to the following:

java -cp "lib\*" org.apache.derby.drda.NetworkServerControl start

The wildcard * is required to load JAR files. When only specifying a directory name, without a wildcard, JAR files in that directory will not be added to the ClassPath, only .class files will be added.