简体   繁体   中英

get JAVA_OPTS at runtime

My app is deployed on Tomcat and I've configured the JAVA_OPTS environment variable in /etc/default/tomcat7 .

There seem to be a million different places where these variables can be provided to Tomcat, so I want to check that the values I'm providing are what's actually being used. Is there something I can inspect at runtime to determine the value of this variable. I checked System.getProperties() , but it doesn't seem to be there.

If you're looking for just the property overrides and JVM arguments, you can use RuntimeMXBean :

RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getInputArguments());

For example, running with the following command-line:

java -Xms512m -Xmx1024m -Dtest.prop=foo com.example.sandbox.RuntimeMXBeanExample

I get the following output:

[-Xms512m, -Xmx1024m, -Dtest.prop=foo]

Note that this does not include arguments passed to the main() method.

You can use System.getenv("JAVA_OPTS") as suggested.

If you don't want to modify code than you can use some of those methods

Java Tools

  1. jps -v displays Java processes with arguments

  2. jvisualvm connects to Java process and let's you inspect number of properties including MXBeans

GNU/Linux tools

  1. ps e displays environment variables passed to processes

One important thing, although post is old. Whatever variable you are passing it should be before the class name you are executing else it will be ignored.

Below will work:

Example: java -classpath . -Dformat=xls -DTabname=\\"Base data new\\" com.cg.bench.GenerateReport

Below will NOT work:

WRONG Example: java -classpath . com.cg.bench.GenerateReport -Dformat=xls -DTabname=\\"Base data new\\"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM