0

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

  • [Yes you can](https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#java-command-line-argument-files) – Federico klez Culloca May 12 '22 at 10:33
  • Thank you for the Answer . can you please give me example for the same i am not able to achieve this. I can easily pass system properties i.e properties that we specify using -D=value but not the vm args like heap size and other gc args .it will be great if you can share any example for the same – Reeta Aggarwal May 12 '22 at 10:51

2 Answers2

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

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
-1

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
$  
Franck
  • 1,340
  • 2
  • 3
  • 15
  • Thank you for your answer, I just tried using java @myargumentsfile , but it is not working for me .java considers it as main class but not the property file name and throws Error myargumentfile main class not found,Please suggest . – Reeta Aggarwal May 12 '22 at 12:23
  • @ReetaAggarwal In `java @myargumentsfile Prop myProp.properties` the `Prop` is the name of the compiled class which contains the `main` method. Your error message indicates that this part was missing in your invocation. – Franck May 12 '22 at 13:47
  • hi Franck,thank you so much for you response i am using java8 . i had tried the way you have mentioned above .but still issue is coming .i think this feature is supported in later version of java .can you please confirm , if yes can you please suggest how can i achieve this with java8. – Reeta Aggarwal May 13 '22 at 05:31
  • @ReetaAggarwal It was introduced with Java 9, https://docs.oracle.com/javase/9/tools/java.htm#JSWOR-GUID-4856361B-8BFD-4964-AE84-121F5F6CF111 – Franck May 13 '22 at 08:24
  • @ReetaAggarwal For non-java-specific solutions see: https://stackoverflow.com/a/28065618/18980756 – Franck May 13 '22 at 08:28
  • @ReetaAggarwal Not a property file but something else, i.e. the environment variable `JAVA_OPTIONS`, see https://stackoverflow.com/questions/17781405/information-about-java-options – Franck May 13 '22 at 08:48
  • For Java 8 I only see the `JAVA_OPTIONS` way. `cat myargumentfile| xargs java` lets the vm arguments appear in `ps`. – Franck May 13 '22 at 09:04
  • i tried this but it didnot work too , thank you so much for your response – Reeta Aggarwal May 20 '22 at 11:45