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
$