0

I need to get the JVM Arguments of a specific Java Program running in the background, how do you exactly do this?

I found out, that the classes ManagementFactory and RuntimeMXBean would give me the needed result. Now I need to do this with a specific program.

Another problem is that I don't get the XMX and XMS properties when running the following code.

RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
List<String> jvmArgs = runtimeMXBean.getInputArguments();

I only get the javaagent and Dfile properties, but I also need the XMX and XMS like I said.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
josef
  • 11
  • 4
  • Why do you need -Xms, -Xmx and other parameters? – Ashish Bhosle Sep 04 '19 at 07:47
  • command line `jps -v` (even with more options)? More at [Tools Reference](https://www.oracle.com/pls/topic/lookup?ctx=javase12&id=JSWOR-GUID-55DE52DF-5774-4AAB-B334-E026FBAE6F34) – user85421 Sep 04 '19 at 07:47
  • It seems like you don't provide xmx xms arguments when you run the program. When I run your code I can get the xmx and xms values – lakshman Sep 04 '19 at 07:50
  • I need to create a tool, which shows the startup properties of a program, which includes the xms and xmx parameters, I guess to see the memory used by jvm and so on – josef Sep 04 '19 at 07:51
  • 2
    You can get the entire command line of any process directly from the operating system, e.g. with `ps` under Unix. Why do you think you need a Java-specific tool? – user207421 Sep 04 '19 at 08:21
  • If you add the flag `-XX:+PrintCommandLineFlags` when you **start** your java process, the first thing logged will be the command line parameters you have in effect (which may not be exactly the parameters that started the process). – DanielBarbarian Sep 04 '19 at 08:22
  • @user207421 I need to make a tool for someone for a program, which shows all the trivial properties of it, I also need to include the jvm properties. I figured out, that you can use the command line for this, but how do I transfer this to a java code? – josef Sep 04 '19 at 08:29
  • @DanielBarbarian Where do I need to run this command line? I read that I need to run it via Jstat but where do I aquire this? – josef Sep 04 '19 at 08:47
  • 1
    In the question’s title, as well as in [this comment](https://stackoverflow.com/questions/57783631/get-jvm-properties-of-a-specific-program#comment102002418_57783631), you are saying “properties”, at all other places, you’re talking about command line arguments. You should try to be consistent regarding what you actually want. – Holger Sep 04 '19 at 09:28
  • As I understood you can use bash scripts for this requirement. Make use of jps, ps, cut, awk etc commands. – Ashish Bhosle Sep 04 '19 at 09:31
  • See [this question](https://stackoverflow.com/questions/55670135/how-do-jps-jinfo-jstat-jmap-and-jstack-get-information-about-local-java-proce) and [this JavaMagazine article](http://www.javamagazine.mozaicreader.com/JanFeb2017#&pageSet=29&page=0&contentItem=0). – apangin Sep 04 '19 at 10:01
  • @josef In order to use that you need to have it as part of the parameters when you start up the java process you are running. Meaning you need to include that parameter when you start the Java Program. – DanielBarbarian Sep 04 '19 at 10:39
  • Ok, I finally found out how to get the arguments from another process, I create a variable of the class Virtual Maschine with the attached PID, then I can read the arguments from the variable. The question now is, how do I find out, what PID the program has through its characeristics? Since the PID always changes. @DanielBarbarian thanks for the link of the article, it really helped! – josef Sep 04 '19 at 10:47
  • See [this answer](https://stackoverflow.com/a/19400008/2711488) for a template to get whatever you like, properties, command line, or actual memory configuration. Since the Attach API allows you to read the system properties of the particular Java process, you may look for particular values that indicate whether it is the right program. If it is your own program, it may even do a `System.setProperty` with a key/value combination of your choice to allow identifying it. – Holger Sep 04 '19 at 11:21
  • I found out, that I need JDK version 9 to utilize the Virtual Maschine class, but I'm forced to use version 8, is there a way around this? – josef Sep 04 '19 at 11:33
  • There is `VirtualMachine` on JDK 8, too. Just include `tools.jar` in the class path. – apangin Sep 04 '19 at 13:07
  • @apangin Ah thats nice, is there a way to do this with getting the PID? For example Runtime.getRuntime().exec("test.exe").pid? Since it seems like I also need jdk 9 for this – josef Sep 06 '19 at 08:10
  • There’s no supported way of getting the pid for a `Process` in Java 8. But as hinted in my previous comment, when you launch the JVM subprocess yourself, you can use a system property, e.g. specify the argument `-Dchoose.your.magic.key=some.generated.uuid` when launching, then, use the Attach API to go through all JVMs and read their system properties to see which one has the key. Or you go the route of using deep Reflection to read the pid from the internal (then, you should check whether you’re on Java 9+ to use the official way then and only hack when it’s the known old version). – Holger Sep 06 '19 at 13:45

1 Answers1

1

A distinct non-answer:

I need to get the JVM Arguments of a specific Java Programm running in the background, how do you exactly do this?

In general, you can't.

You see, as soon as that process is running, all the parameter passed to it were "consumed", and it is totally up to that process what happens about that.

What I mean is: there is no such thing as a "universal" JVM any more in the first place. There are actually different implementations by now. Sure, they are all supposed to support the "standardised" -X options. But each and any JVM implementation is allowed to provide "its own" options and switches.

Long story short: there is no universal way to acquire "the parameters passed" from a running JVM.

If you really care about this, I suggest: write your own java wrapper script that simply logs all command line parameters into some sort of journal, ideally with a timestamp and the process ID of the "actual" java that your wrapper script invoked with those parameters.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I startet Java VisualVM, which was located in the java jdk: When running 2 diffrent java programs, I can see 2 diffrent processes starting with org. with the needed arguments, in the VisualVM program. Is there no way to output the properties of one process? – josef Sep 04 '19 at 09:23
  • If I have the PID from my program, couldn't i use this to spot it, and print out the JVM arguments? – josef Sep 04 '19 at 09:56
  • @josef I think you want to read my answer again. **In general, you can't**. – GhostCat Sep 04 '19 at 10:44
  • I think I didn't express myself clearly with my question, since I found a solution for my problem. I create a variable of the Virtual Maschine class with the attached PID then I can find the arguments I need by printing it out. – josef Sep 04 '19 at 10:54