java Command

The java command runs a Java program from a command prompt. The basic syntax is

java filename [options]

When you run the java command, the JRE is loaded along with the class you specify. Then, the main method of that class is executed.

Here’s an example that runs a program named HelloApp:

C:\java\samples>java HelloApp

tip.eps The bold indicates the part you type.

Remember.eps The class must be contained in a file with the same name as the class, and its filename must have the extension .class. You typically don’t have to worry about the name of the class file because it’s created automatically when you compile the program with the javac command.

The Java runtime command lets you specify options that can influence its behavior.

Option

Description

-? or -help

Lists standard options

-classpath directories and archives

Lists the directories or JAR or Zip archive files used to search for class files

-client

Runs the client virtual machine

-cp <search path>

Does the same thing as -classpath

-D name=value

Sets a system property

-dsa or -disablesystemassertions

Disables system assertions

-ea classes or packages

Enables assertions for the specified classes or packages

-ea or -enableassertions

Enables the assert command

-esa or ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.