9 Things I Regret Not Knowing About Running Java JAR Applications
Essential Commands and Options Explained
I remember having to setup a github workflow for a personal project and sorry to say, it wasn’t so much fun at the time, but I did stumble on these different commands after looking through the internet to figure out a command that worked for my use case.
I’ll start with the basics and gradually introduce more advanced options.
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
).
This basic command might not be enough in production environments, where you need to account for memory constraints, logging, security, and long-running processes.
2. Running in Background with nohup
If you want to run the Java application in the background, allowing it to keep running even after you log out from your terminal, use nohup
(no hangup):
nohup java -jar myapp.jar &
nohup
prevents the process from being terminated when the terminal session ends.&
sends the process to the background, freeing up your terminal for other tasks.
Example: 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.
3. Using sudo
for Permission-Sensitive Environments
In certain cases, you may need to run your Java application with root privileges, especially if it accesses restricted files or network ports (e.g., below port 1024).
sudo java -jar myapp.jar
Note: Only use sudo
if absolutely necessary, as it can introduce security risks.
Example: Running as Root in the Background
sudo nohup java -Xmx1024m -jar myapp.jar &
This command runs the Java application with root privileges, prevents it from terminating on logout, and allocates a maximum of 1024 MB heap memory.
4. 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. Logging Output with nohup
When using nohup
, the output of the command is typically redirected to nohup.out
in the current directory. However, you can specify a different log file by redirecting the output.
nohup java -Xmx1024m -jar myapp.jar > myapp.log 2>&1 &
In this example:
> myapp.log
redirects the standard output tomyapp.log
.2>&1
combines standard error with standard output, ensuring all messages go to the same log file.
6. 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.
7. 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.
8. 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.
9. 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.
Conclusion
Running Java applications with the right commands and options can make a significant difference in reliability, performance, and maintenance. By understanding and applying these options, you can tailor your application’s runtime environment to meet production standards, making it robust and easier to manage.