Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally setting properties for JVM [duplicate]

Is there any way to set certain properties that will be applicable for all java processes(java.exe,javaw.exe for windows) running on that machine ?

More clearly suppose I want to use a specific timezone to be applied for all java processes running in that machine(without changing the system timezone).

I know we can pass it as -D argument, but it will be applicable only for that java process right.But I need it in the other way- for every java process started.

Is there any way to do that?

like image 954
Tom Sebastian Avatar asked Nov 18 '15 13:11

Tom Sebastian


People also ask

How do I set JVM system properties in Windows?

System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System. setProperty(String key, String value) or via the various System. getProperties().

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.

How can I see JVM properties?

You can determine the properties and variables of your JVM by determining the process id of java (ps -ef, jps, or task manager), cd'ing to $JAVA_HOME/bin directory, then running jinfo <process id> . Of course you can use grep to find a specific property.


1 Answers

Tried with Java 8:

You can use the environment variable _JAVA_OPTIONS to specify default parameters for java.exe and javaw.exe, for example

C:> set _JAVA_OPTIONS=-Dfile.encoding=UTF-8

Quick and dirty test for javaw:

package com.example;

import javax.swing.JFrame;
import javax.swing.JTextPane;

public class PropertyTest {

    public static void main(String[] args) {
        String value = (String) System.getProperties().get("file.encoding");

        JTextPane txt = new JTextPane();
        txt.setText(value);
        JFrame main = new JFrame();
        main.add(txt);
        main.setVisible(true);
    }
}
C:> javaw com.example.PropertyTest

enter image description here

C:> set _JAVA_OPTIONS=-Dfile.encoding=UTF-8
C:> javaw com.example.PropertyTest

enter image description here

By setting the environment variable as a system environment variable, it applies to all java processes.

See also

  • http://www.rgagnon.com/javadetails/java-set-java-properties-system-wide.html which specifies some additional environment variables (which I have not tested).
  • Java System Environment Variable on StackOverflow
like image 103
Andreas Fester Avatar answered Sep 20 '22 12:09

Andreas Fester