Skip to content

Instantly share code, notes, and snippets.

@rajendarreddyj
Created February 21, 2025 15:31
Show Gist options
  • Save rajendarreddyj/e1841e63f1bad8a3ae6bc82c93862ef0 to your computer and use it in GitHub Desktop.
Save rajendarreddyj/e1841e63f1bad8a3ae6bc82c93862ef0 to your computer and use it in GitHub Desktop.

When configuring Apache Tomcat, you can use multiple mechanisms to set JVM parameters, system properties, and environment variables. Each has a specific role and scope.

#Catalina Options (CATALINA_OPTS)

βœ… Purpose:

  • Used for setting JVM options specific to Tomcat runtime (startup and shutdown).
  • Typically configured in setenv.sh (Linux/macOS) or setenv.bat (Windows).

βœ… Common Uses:

  • Memory settings (Heap, GC tuning)
  • Debugging options
  • JMX remote management

βœ… Example:

export CATALINA_OPTS="-Xms512m -Xmx1024m -XX:+UseG1GC -Dmyapp.config=/etc/myapp/config"

πŸ“Œ Important:

  • Only affects Tomcat (not other Java applications).
  • Used for both startup (catalina.sh start) and shutdown (catalina.sh stop).
  • Typically used in setenv.sh / setenv.bat to ensure it persists across restarts.

Java Options (JAVA_OPTS)

βœ… Purpose:

  • A generic JVM options variable that applies to any Java application, including Tomcat.
  • Can be used for JVM memory settings, garbage collection tuning, logging, and debugging.

βœ… Common Uses:

  • Defining JVM memory settings
  • Enabling remote debugging (-agentlib:jdwp)
  • Configuring Java security

βœ… Example:

export JAVA_OPTS="-Xms256m -Xmx512m -Djava.security.egd=file:/dev/./urandom"

πŸ“Œ Important:

  • Affects all Java applications launched in the same environment.
  • If set in Tomcat, it applies to both Catalina and Bootstrap classes.
  • Overridden by CATALINA_OPTS if both are set.

Environment Variables

πŸ”Ή What is it?

Environment variables are system-wide variables that can be accessed by Tomcat, Java applications, or other system processes.

πŸ”Ή Key Characteristics

  • Set at the operating system level.
  • Can be accessed in Java using System.getenv("VARIABLE_NAME").
  • Affects all processes on the system.

πŸ”Ή Usage Example

Linux/macOS

export DB_URL="jdbc:mysql://localhost:3306/mydb" export LOG_LEVEL="DEBUG"

Windows (PowerShell)

$env:DB_URL="jdbc:mysql://localhost:3306/mydb"

πŸ”Ή Common Use Cases

βœ… Storing database credentials:

export DB_URL="jdbc:mysql://localhost:3306/mydb"

βœ… Setting log levels:

export LOG_LEVEL="DEBUG"

βœ… Defining global application settings:

export APP_ENV="production"

❗ Security Considerations

⚠️ Do not store sensitive credentials (like passwords or API keys) in environment variables without proper security measures.
⚠️ They are accessible by all processes running on the machine.

System Properties (-Dproperty=value)

βœ… Purpose:

  • JVM arguments used to pass configuration values to a specific Java application.
  • Used for defining application-level settings, logging levels, or feature toggles.

βœ… Common Uses:

  • Custom application configuration (-Dmyapp.config=/etc/myapp/config)
  • Logging settings (-Dlog4j.configuration=file:/path/to/log4j.xml)
  • Security properties (-Djava.security.egd=file:/dev/./urandom)

βœ… Example:

java -Dapp.env=production -Dserver.port=8080 -jar myapp.jar

πŸ“Œ Important:

  • Only applies to the specific Java application when it's launched.
  • Can be accessed using System.getProperty("app.env") in Java code.

Comparison Table

Feature Scope Used in Example
CATALINA_OPTS Tomcat-specific Tomcat startup (catalina.sh) export CATALINA_OPTS="-Xmx1024m -XX:+UseG1GC"
JAVA_OPTS Generic JVM options Any Java application (including Tomcat) export JAVA_OPTS="-Xms512m -Xmx1024m"
Environment Variables System-wide Any process or application export DB_URL="jdbc:mysql://localhost:3306/db"
System Properties (-D**)** Java application-specific Passed at runtime (java -Dproperty=value) java -Dlog.level=DEBUG -jar myapp.jar

Best Practices

  1. Use CATALINA_OPTS for Tomcat-specific configurations (memory, debugging, JMX).
  2. Use JAVA_OPTS for generic JVM configurations that might apply beyond Tomcat.
  3. Use environment variables for system-wide settings like database URLs.
  4. Use -D system properties for application-specific settings that need to be accessed via System.getProperty().

Example: Configuring Tomcat with All Four Mechanisms

Linux/macOS (setenv.sh):

export JAVA_HOME="/usr/lib/jvm/java-11-openjdk" # Environment variable
export CATALINA_OPTS="-Xms512m -Xmx1024m -Dapp.mode=production"
export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"
export DB_URL="jdbc:mysql://localhost:3306/mydb" # Environment variable

Windows (setenv.bat):

set CATALINA_OPTS=-Xms512m -Xmx1024m -Dapp.mode=production
set JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom
set DB_URL=jdbc:mysql://localhost:3306/mydb

How They Work in Code

Accessing Environment Variables in Java:

String dbUrl = System.getenv("DB_URL"); System.out.println("Database URL: " + dbUrl);

Accessing System Properties (-D options):

String mode = System.getProperty("app.mode"); System.out.println("Application Mode: " + mode);

Final Takeaway

  • Use CATALINA_OPTS for Tomcat-specific settings.
  • Use JAVA_OPTS for global JVM configurations.
  • Use environment variables for system-wide settings.
  • Use -D system properties for application-level configurations.

Catalina Policy Restrictions on Accessing Environment Variables in Tomcat

By default, Apache Tomcat's Security Manager restricts access to environment variables for security reasons. These restrictions are enforced through the catalina.policy file, which controls permissions granted to web applications.

Why Are Environment Variables Restricted?

Tomcat runs under a Security Manager when enabled, which prevents applications from accessing system properties and environment variables unless explicitly permitted. This is to prevent potential security risks, such as:

  1. Leaking sensitive information (e.g., database credentials, API keys).
  2. Preventing unauthorized code execution (e.g., accessing PATH variables or executing system commands).
  3. Restricting file system access (ensuring applications don’t read or modify system files).

How to Allow Access to Environment Variables in catalina.policy

Step 1: Locate the catalina.policy File

You can find the catalina.policy file in the $CATALINA_HOME/conf/ directory:

  • Linux/macOS: /opt/tomcat/conf/catalina.policy
  • Windows: C:\Tomcat\conf\catalina.policy

Step 2: Grant Permissions to Access Environment Variables

To allow applications to access environment variables, add the following permissions to catalina.policy:

grant codeBase "file:${catalina.base}/webapps/myapp/-" {
  permission java.util.PropertyPermission "env.\*", "read";
};

βœ… Explanation:

  • The grant block applies to all code loaded from the myapp web application.
  • java.util.PropertyPermission "env.*", "read"; allows reading all environment variables.
  • If you only need specific variables, replace "env.*" with the exact variable name, e.g.:
permission java.util.PropertyPermission "env.DB_URL", "read";
permission java.util.PropertyPermission "env.SECRET_KEY", "read";

πŸ“Œ Important:

  • Modifying catalina.policy requires restarting Tomcat for changes to take effect.
  • Be cautious when granting access to sensitive environment variables.

Step 3: Restart Tomcat

After updating catalina.policy, restart Tomcat to apply changes:

systemctl restart tomcat # Linux (if running as a service)
``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment