Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a java system property so that it is effective whenever I start JVM without adding it to the command line arguments

There was a change in Java 1.7 in the way the default Locale is get from the OS. There are methods of restoring the old behaviour e.g. by setting the flag -Dsun.locale.formatasdefault=true when starting a JVM instance.

I would like to set this flag permanently so that I don't have to specify it in command line arguments each time when I start a JVM instance. Is there a file or any other possibility to change the default settings for JVM? Something like the Eclipse.ini file but for the JVM itself?

like image 875
Bug Pecker Avatar asked Sep 25 '12 06:09

Bug Pecker


People also ask

How do I set JVM system properties?

For pooled JVMs, you can also set these options in a JVM properties file to share the same options between different profiles. To reference the file, use the JVMPROPS option in each JVM profile. CICS passes all the system properties in a JVM profile or JVM properties file to the JVM unchanged.

How do I get system properties in Java?

To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.

Where are JVM properties set?

The directory server provides a means of configuring the Java Virtual Machine (JVM) and Java options for each command-line utility and for the directory server itself. The Java configuration is provided in a properties file, located at instance-dir /OUD/config/java.


1 Answers

You can set set environment variable JAVA_TOOL_OPTIONS in your OS. All Java tools (java, javac, ..) will pick this variable up and use it. So you could e.g. use

SET JAVA_TOOL_OPTIONS=-Dsun.locale.formatasdefault=true 

I use this to force a specific locale for each JVM.

But this only works if your application is started through the Java tools. If it is e.g. started from a C program that calls the jvm DLL this won't be used.

Edit: I just tested it, and it seems JAVA_TOOLS_OPTIONS is also picked up when the DLLs are started (verified with a Swing application that uses WinRun4J as a launcher)

See: http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/envvars.html

like image 75
a_horse_with_no_name Avatar answered Sep 29 '22 13:09

a_horse_with_no_name