9 Things I Regret Not Knowing About Running Java JAR Applications
1. Basic Command for Running a JAR File
The simplest way to run a JAR file is by using the java -jar
command:
java -jar myapp.jar
This command instructs the Java Virtual Machine (JVM) to execute the JAR file specified (in this case, myapp.jar
).
2.Running with Memory Settings
nohup java -Xmx1024m -jar myapp.jar &
In this example, the JVM is also given a maximum heap size of 1024 MB (-Xmx1024m
). Adjusting the memory settings is a good idea to prevent OutOfMemoryErrors, particularly with large applications.
This basic command might not be enough in production environments, where you need to account for memory constraints, logging, security, and long-running processes.
3. Setting Java Options for Performance Optimization
Java provides numerous options to tune the performance of the JVM, depending on your application’s needs. Common ones include:
- Heap Size (
-Xmx
and-Xms
): Adjust the maximum (-Xmx
) and initial (-Xms
) heap sizes. For example:
java -Xms512m -Xmx1024m -jar myapp.jar
This sets the initial heap size to 512 MB and the maximum heap size to 1024 MB.
Garbage Collection: Specify the garbage collection algorithm with flags such as
-XX:+UseG1GC
(recommended for large applications) or-XX:+UseParallelGC
(for multi-threaded applications).
java -XX:+UseG1GC -jar myapp.jar
5. Environment Variables for JVM Configuration
Sometimes, it’s more convenient to set environment variables for JVM options instead of passing them directly in the command. This is especially useful if you have multiple Java applications on the same server and want to configure each differently.
Example: Setting Environment Variables
export JAVA_OPTS="-Xms512m -Xmx1024m -XX:+UseG1GC"
java $JAVA_OPTS -jar myapp.jar
This command allows you to reuse JAVA_OPTS
across different applications or sessions without rewriting the flags each time.
6. Debugging Options
If you need to debug your Java application, you can enable remote debugging with the -agentlib:jdwp
option.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar myapp.jar
This command:
Enables debugging on port 5005.
Allows remote debugging tools to connect to your application for monitoring and troubleshooting.
7. Executing the Command in Production: Best Practices
Use
nohup
for Long-Running Processes: This ensures the application continues running even if your terminal session closes.Log Output to a Dedicated File: This makes it easier to monitor and debug by organizing logs per instance.
Set Memory Limits According to Application Needs: This prevents excessive memory usage and optimizes performance.
Test JVM Options: Start with recommended options like
-Xms
,-Xmx
, and-XX:+UseG1GC
, then adjust as needed based on your application’s resource demands.
8. Full Example Command for Production
A final example combining all elements:
sudo nohup java -Xms512m -Xmx2048m -XX:+UseG1GC -jar myapp.jar > /var/log/myapp.log 2>&1 &
This command:
Runs the Java application with root privileges (
sudo
).Allocates initial (512 MB) and maximum (2048 MB) heap sizes.
Uses the G1 garbage collector.
Runs in the background and redirects all output to a specified log file.
Subscribe to my newsletter
Read articles from Luc Cao directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by