杰瑞科技汇

shell java jar

Of course! The term "shell java jar" refers to the common practice of running a Java application (packaged in a .jar file) from a command-line shell (like Bash on Linux/macOS or PowerShell/CMD on Windows).

Let's break this down into the essential components and provide a comprehensive guide.


The Core Components

a) Shell (Terminal / Command-Line Interface)

This is the program that allows you to interact with your operating system by typing commands. Examples include:

  • Windows: Command Prompt (cmd.exe), PowerShell (powershell.exe), Windows Terminal.
  • Linux: Bash, Zsh, etc.
  • macOS: Terminal (which uses Zsh by default).

b) Java

Java is a programming language and a platform. To run a .jar file, you need the Java Runtime Environment (JRE) installed on your system. This includes the java command-line tool.

  • Check if Java is installed:
    java -version

    If it's installed, you'll see the version number. If not, you'll need to download and install Java.

c) JAR (Java Archive)

A .jar file is a package format that contains compiled Java bytecode (.class files), metadata, and resources (images, text files, etc.) needed to run a Java application. It's essentially a ZIP file with a .jar extension and a special META-INF/MANIFEST.MF file.


The Basic Command: java -jar

The fundamental command to run a Java application from a JAR file is:

java -jar your_application.jar

How it works:

  • java: The command to invoke the Java Virtual Machine (JVM).
  • -jar: A flag that tells the java command that you want to run a program from a JAR file. When you use this flag, the JVM automatically looks for the Main-Class inside the JAR's manifest file to know where to start execution.

Advanced Options and Common Scenarios

In practice, you often need more control. Here are the most common and important options to add to your command.

a) Setting the Main Class Manually

If your JAR doesn't have a Main-Class in its manifest (or you want to override it), you can specify it directly using the -cp (classpath) and -main options.

# Syntax
java -cp your_application.jar com.yourcompany.MainClass
# Example
java -cp my-app.jar com.example.server.ServerStarter
  • -cp (or -classpath): Specifies the location of the classes and resources the JVM should use. Here, it's just the JAR file itself.

b) Setting JVM Memory (Crucial for Performance)

For any non-trivial application, you'll need to allocate more memory to the JVM.

  • Initial Heap Size (-Xms): The amount of memory allocated to the JVM when it starts.
  • Maximum Heap Size (-Xmx): The maximum amount of memory the JVM can use.

Example:

# Allocate 1GB of initial memory and 4GB of maximum memory
java -Xms1g -Xmx4g -jar your_application.jar

Pro Tip: If you get an OutOfMemoryError, the first thing to do is increase the -Xmx value.

c) Passing Arguments to Your Application

You can pass command-line arguments to your Java application. These are simply added after the JAR file.

Example:

# The application inside my-app.jar will receive these arguments
java -jar my-app.jar --server.port=8080 --spring.profiles.active=prod

Your Java code would then parse these arguments (e.g., using a library like Apache Commons CLI or Spring Boot's @Value).

d) Running in the Background

To run the application as a background process (so you can close the terminal and it keeps running), use &.

java -jar your_application.jar &

Note: The process will be terminated if you log out or close the terminal. For true background services, you should use a process manager like systemd (Linux) or NSSM (Windows).

e) Capturing Output and Errors

By default, the output of a background process (&) can be hard to manage. You can redirect it to files.

# Run in the background, redirect standard output to app.log and standard error to error.log
nohup java -jar your_application.jar > app.log 2> error.log &
  • nohup: A command that makes the process immune to "hangup" signals, meaning it won't be terminated when you log out.
  • >: Redirects standard output.
  • 2>: Redirects standard error.

Practical Example: A Spring Boot Application

Let's say you have a Spring Boot application named my-web-app.jar and you want to:

  1. Give it 2GB of RAM.
  2. Run it on port 8080.
  3. Pass a profile argument.
  4. Run it in the background and log output.

Your command would look like this:

# Run with memory, argument, background, and logging
nohup java -Xms2g -Xmx2g -jar my-web-app.jar --spring.profiles.active=production > web_app.log 2>&1 &
  • 2>&1: This is a useful trick that redirects standard error (2) to the same place as standard output (1), so both logs go into web_app.log.

Creating an Executable JAR (with a Main-Class)

For the java -jar command to work without manually specifying the class, your JAR must have a Main-Class in its META-INF/MANIFEST.MF file.

When building a project with Maven or Gradle, this is usually done automatically.

Maven (pom.xml): The maven-jar-plugin handles this. You specify the main class in the plugin configuration.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.yourcompany.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Gradle (build.gradle): The application plugin makes this easy.

plugins {
    id 'java'
    id 'application'
}
application {
    mainClass = 'com.yourcompany.MainClass'
}

After building with mvn package or gradle build, the resulting JAR will be runnable with java -jar your-app.jar.

Summary Table

Goal Command Example
Run a basic JAR java -jar my-app.jar
Specify the main class java -cp my-app.jar com.example.Main
Set memory (1GB start, 4GB max) java -Xms1g -Xmx4g -jar my-app.jar
Pass arguments to the app java -jar my-app.jar --config=prod
Run in the background java -jar my-app.jar &
Run in background & log everything nohup java -jar my-app.jar > app.log 2>&1 &
Check if a process is running (Linux) ps aux | grep java or pgrep -f "my-app.jar"
Stop a running process (Linux) pkill -f "my-app.jar" or kill <process_id>
分享:
扫描分享到社交APP
上一篇
下一篇