杰瑞科技汇

Java已启动,如何确认服务正常运行?

Excellent! The Java Virtual Machine (JVM) is now running and ready for your commands.

Java已启动,如何确认服务正常运行?-图1
(图片来源网络,侵删)

Here’s a breakdown of what "java is started" means and what you can do next.

What Happened When You Started Java?

When you type java in your terminal and press Enter, you're executing the Java launcher, which is part of the Java Development Kit (JDK) or Java Runtime Environment (JRE).

This launcher does a few key things:

  1. Finds the JVM: It locates the jvm.dll (on Windows) or libjvm.so (on Linux/macOS) file, which is the actual virtual machine.
  2. Loads the JVM: It loads this native library into memory. This is the most resource-intensive part of the startup process.
  3. Initializes the JVM: The JVM sets up its internal memory areas (like the method area and heap), starts the class loader, and prepares to execute Java bytecode.
  4. Enters a "Live" State: The JVM is now active and waiting for instructions. It's consuming memory and CPU cycles, even if it's not doing any "useful" work yet.

What Can You Do Now?

You are now at the Java command prompt. You can run Java programs directly from here without needing to type java again.

Java已启动,如何确认服务正常运行?-图2
(图片来源网络,侵删)

Check Java Version (A Quick Sanity Check)

It's always a good idea to confirm the version you're running.

version

You should see output similar to this, confirming the JVM is active:

java version "17.0.8" 2025-07-18
Java(TM) SE Runtime Environment (build 17.0.8+9-LTS-211)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.8+9-LTS-211, mixed mode, sharing)

Run a Java Class File

If you have a compiled Java class (a .class file), you can run it directly. Let's say you have a file named MyProgram.class in the current directory.

MyProgram

The JVM will look for the main method in MyProgram.class and execute it.

Java已启动,如何确认服务正常运行?-图3
(图片来源网络,侵删)

Run a Java Archive (JAR) File

This is very common for running applications. If you have a my-app.jar file, you can run it like this:

-jar my-app.jar

The -jar flag tells the JVM to look inside the JAR file for a special manifest file that specifies which class contains the main method to run.

Use the REPL (Read-Eval-Print Loop)

Java 9 and later includes a built-in REPL called jshell. It's an interactive command-line tool that lets you execute Java code snippets without creating a full file.

Just type jshell:

jshell

You'll see a welcome message and a > prompt. You can now write Java code directly.

jshell> System.out.println("Hello from a live JVM!");
Hello from a live JVM!
jshell> int a = 10;
a ==> 10
jshell> int b = 20;
b ==> 20
jshell> System.out.println("a + b = " + (a + b));
a + b = 30

To exit the REPL and return to your normal terminal, type /exit.

Exit the Java Environment

When you are finished, you should exit the Java environment to free up the resources (memory, CPU) that the JVM is using.

exit

This will shut down the JVM gracefully and return you to your standard system command prompt.


How to Start Java with Options

You can also provide options when you start Java to control its behavior. For example:

  • Set the Maximum Heap Size: This is one of the most common uses.

    -Xmx2g MyProgram

    This tells the JVM to allow a maximum heap size of 2 gigabytes for your application.

  • Set the Initial Heap Size:

    -Xms512m -Xmx2g MyProgram

    This sets the initial heap size to 512 MB and the maximum to 2 GB.

  • Enable Verbose Garbage Collection: Useful for debugging memory issues.

    -verbose:gc MyProgram

    This will print detailed messages to the console every time the garbage collector runs.

Summary

Command Action
java Starts the JVM and opens the Java command prompt.
version (Inside Java prompt) Displays the Java version.
MyProgram (Inside Java prompt) Runs a compiled Java class.
-jar my-app.jar (Inside Java prompt) Runs a Java application from a JAR file.
jshell (Inside Java prompt) Starts the interactive Java REPL.
exit Exits the Java environment and shuts down the JVM.
分享:
扫描分享到社交APP
上一篇
下一篇