Batch and Java Windows: Combining -XX: OnOutOfMemoryError Commands and Batch Parameters

I am completely new to Windows batch programming. I want to achieve a startup script for a Java application. However, it does not start the Java application, but prints

Usage: java [-options] class [args ...]

(to execute the class)

or java [-options] -jar jarfile [args ...]

(to execute jar file)

...

which indicates that my settings are not correctly recognized.


Here is my MCVE for a broken script:

set memory=600000 java -XX:OnOutOfMemoryError="taskkill /PID %p" -Xmx%memory%K -jar MyApp.jar 

In a real scenario, memory calculated to set the optimal maximum heap size for the application.


Leaving one of two parameters, the application starts. So

 java -XX:OnOutOfMemoryError="taskkill /PID %p" -jar MyApp.jar 

and

 set memory=600000 java -Xmx%memory%K -jar MyApp.jar 

works, but I need both parameters to work in one call.

+5
source share
1 answer

If you put @echo on and @echo off in your script, you will see which command is executed.

You will say that the final command:

 java -XX:OnOutOfMemoryError="taskkill /PID memoryK -jar MyApp.jar 

which makes it look like % of %p , which is seen as "opening" % for substitution.

Besides the fact that the quotation marks are not balanced, this means that you are not actually giving the java command to run - -jar MyApp.jar actually inside the line -XX:OnOutOfMemoryError .

Try replacing a single % with %% - according to this question , that is, how to write the % literal in a BAT file:

 java -XX:OnOutOfMemoryError="taskkill /PID %%p" -Xmx%memory%K -jar MyApp.jar 
+2
source

Source: https://habr.com/ru/post/1245022/


All Articles