'can I pass java vm args i.e. -xms and -xmx through a property file to my java program

Can anybody help me in following problem:

if I am specifying java args while running my program through java command then if I check my java process using ps -ef command it shows me a long output including all vm arguments .I just want to pass a single property file as system argument and then want to read all vm and program arguments from it ,so that ps command for my process only shows config file in path and ps output is shorten



Solution 1:[1]

You can use a command line arguments file.

Let's say you have a file called args (an arbitrary name) containing your arguments

-Xmn25m
-Xmx51m
-XX:+UseParallelGC

And a java program that, for example, prints the maximum size for the heap

public class ArgsTest {
    public static void main(String args[]) {
        System.out.println(Runtime.getRuntime().maxMemory());
    }
}

You can pass the arguments in the file by prefixing the file name with @, so for example running the program this way

java @args ArgTest.java

prints 51380224

Let's change the arguments file to increase the heap maximum size to 512 MB

-Xmn25m
-Xmx512m
-XX:+UseParallelG

The same program run with the same command line now prints 528482304

Solution 2:[2]

From Java 9 onwards VM parameters can be put into a java Command-Line Argument File (https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#java-command-line-argument-files, Link from Frederico).

Info on option Xms:

-Xms size
Sets the minimum and the initial size (in bytes) of the heap.
This value must be a multiple of 1024 and greater than 1 MB. 
Append the letter k or K to indicate kilobytes, m or M to
indicate megabytes, or g or G to indicate gigabytes. 
The following examples show how to set the size of allocated
memory to 6 MB using various units:

-Xms6291456
-Xms6144k
-Xms6m

java command-line argument file (myargumentfile) contains only one line:

-Xms10m

java call:

java @myargumentfile <Class Name>

To hide other command line parameters by using a property file and to show the input parameter:

Properties file (myprop.properties) contains two lines:

value1="property value 1"
value2=property value 2

Java source (Prop.java):

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ManagementFactory;
public class Prop {
    public static void main(String[] args) {

        Properties prop = new Properties();
        try {
            // load a properties file from class path, inside static method
            prop.load(Prop.class.getClassLoader().getResourceAsStream(args[0]));

            System.out.println(prop.getProperty("value1"));
            System.out.println(prop.getProperty("value2"));

            RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
            List<String> arguments = RuntimemxBean.getInputArguments();
            arguments.stream().forEach(entry -> System.out.println(entry));              
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

The files myargumentfile, Prop.java and myprop.properties are in the same directory. Prop.class will be generated there.

$ javac Prop.java
$ java @myargumentfile Prop myprop.properties
"property value 1"
property value 2
-Xms10m
$  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Federico klez Culloca
Solution 2