Wednesday, October 23, 2013

I need to check if some option which can be passed to JVM is explicitly set or is it have default value.
To be more specific:
I need to create one specific thread with higher native stack size than the default one, but in case user want to take care of such things by himself by specifying -Xss option I want to create all threads with default stack size (which will be specified by user in -Xss option).
I've checked classes like java.lang.System and java.lang.Runtime, but these aren't giving me information about vmargs.
Is there any way to get information I need?
share|improve this question
 
Next time you log in, could you switch the accepted answer? –  Erick Robertson Dec 5 '11 at 13:09 

3 Answers

up vote51down voteaccepted
With this code you can get the JVM arguments:
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
...
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
share|improve this answer
 
Sadly you cannot get the Name of the main class if it is given on the command line. –  Daniel May 20 '10 at 8:06
 
@Daniel, this should get you the name of the main class: final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); final String mainClassName = stackTrace[stackTrace.length - 1].getClassName()); –  laz Nov 16 '11 at 18:43
 
@laz System.getProperty("sun.java.command") is far simpler. –  Vulcan May 28 '12 at 6:19
1 
If you are guaranteed to be running on Oracle's JVM and have the code to parse the arguments that can be useful. –  laz May 28 '12 at 12:57
 
@Vulcan That does not get VM arguments. It contains the main class name, and the args array to the main method. –  dacongy Sep 24 '12 at 2:10
show 1 more comments
At startup pass this -Dname=value
and then in your code you should use
value=System.getProperty("name");
to get that value
share|improve this answer

No comments: