Reputation: 14270
I need to check if some option that can be passed to JVM is explicitly set or has its 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 the user wants to take care of such things by himself by specifying the -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 any useful information about VM arguments.
Is there any way to get the information I need?
Upvotes: 199
Views: 225717
Reputation: 1110
I found that HotSpot lists all the VM arguments in the management bean except for -client and -server. Thus, if you infer the -client/-server argument from the VM name and add this to the runtime management bean's list, you get the full list of arguments.
Here's the SSCCE:
import java.util.*;
import java.lang.management.ManagementFactory;
class main {
public static void main(final String[] args) {
System.out.println(fullVMArguments());
}
static String fullVMArguments() {
String name = javaVmName();
return (contains(name, "Server") ? "-server "
: contains(name, "Client") ? "-client " : "")
+ joinWithSpace(vmArguments());
}
static List<String> vmArguments() {
return ManagementFactory.getRuntimeMXBean().getInputArguments();
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static String javaVmName() {
return System.getProperty("java.vm.name");
}
static String joinWithSpace(Collection<String> c) {
return join(" ", c);
}
public static String join(String glue, Iterable<String> strings) {
if (strings == null) return "";
StringBuilder buf = new StringBuilder();
Iterator<String> i = strings.iterator();
if (i.hasNext()) {
buf.append(i.next());
while (i.hasNext())
buf.append(glue).append(i.next());
}
return buf.toString();
}
}
Could be made shorter if you want the arguments in a List<String>
.
Final note: We might also want to extend this to handle the rare case of having spaces within command line arguments.
Upvotes: 7
Reputation: 551
If you want the entire command line of your java process, you can use: JvmArguments.java (uses a combination of JNA + /proc to cover most unix implementations)
Upvotes: 2
Reputation: 2425
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();
Upvotes: 231
Reputation: 2593
At startup pass this -Dname=value
and then in your code you should use
value=System.getProperty("name");
to get that value
Upvotes: 232
Reputation: 5224
I haven't tried specifically getting the VM settings, but there is a wealth of information in the JMX utilities specifically the MXBean utilities. This would be where I would start. Hopefully you find something there to help you.
The sun website has a bunch on the technology:
http://java.sun.com/javase/6/docs/technotes/guides/management/mxbeans.html
Upvotes: 3