A Java application is launched with the java command which starts a JVM and runs the application. As an Application support engineer, there are few basic operations that you will typically perform on the JVM. Here are they:
Starting the JVM simply means starting the Java application. A Java application is started using the java command.
java is the command
HelloWorld is the classfile without the .class extension
Note: Don’t use the .class extension in the java command. If you do, you will get an error
[ec2-user@java]$ java HelloWorld.class Error: Could not find or load main class HelloWorld.class
There are numerous command line options for the java command. Here are some of the main ones you need to know.
|Java command line option||Meaning|
|-server||Start JVM in Server mode (default in enterprise class hardware).|
|-version||Show the java version|
|-cp||Classpath. Search path of directories, zip/jar files to search for class files|
|-classpath||Same as -cp|
|-X||Print information on NON-STANDARD options.|
|-javaagent||Load Java agent (typically used by monitoring agents like Appdynamics,dynatrace etc)|
|-D<name>=<value>||Set a Java System Property. Very important option.|
|-verbose:gc||Enable verbose Garbage collection|
For example, to show the java version:
[ec2-user@java]$ java -version openjdk version "1.8.0_101" OpenJDK Runtime Environment (build 1.8.0_101-b13) OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode) In later sections, detailed information about Java command line options and Systems properties are discussed.
There is a programmatic way of stopping a running JVM from within the application. This is achieved using either halt or exit method from runtime class. However, as an Application Support Engineer, you will most probably use some external commands to stop the JVM. If you are using a JEE Application Server such as WebLogic or WebSphere, a stop command is generally provided by the application server. Or you could use the Admin Console of the Application Server. If yours is a standalone application, generally the application developer provides the stop command. You can also use kill -9 to terminate a JVM. But this method is not clean and may corrupt data.
If you just need to verify that the JVM is running, a simple ps command will work:
[ec2-user@~]$ ps -ef|grep java | grep -v grep ec2-user 19306 18880 0 03:59 pts/1 00:00:00 java HelloWorld
In general, to monitor all aspects of a JVM, you will end up using some of the following
- APM Solution (Such as AppDynamics, NewRelic or Dynatrace)
- Java monitoring tools that come with the JDK such as Jconsole,VisualVM
- Native Java commands such as jmap, jstack etc
Monitoring JVM will be discussed in detail later in this course