Poornima
Poornima

Reputation: 61

Unable to generate GC logs in java 11

I am trying to create gc log in java 11 in the folder which does not exist.xxx folder does not exists.

C:\>java -Xlog:gc*:file=C:\Users\xxx\gc.log --version

[0.006s][error][logging] Error opening log file 'C:\Users\xxx\gc.log': No such file or directory
[0.006s][error][logging] Initialization of output 'file=C:\Users\xxx\gc.log' using options '(null)' failed.
Invalid -Xlog option '-Xlog:gc*:file=C:\Users\xxx\gc.log', see error log for details.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

How to get the gc logs in a folder which does not exist? If the folder exists, it works fine.

java -version 

openjdk version "11.0.5" 2019-10-15 
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10) 
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)

Upvotes: 3

Views: 4485

Answers (1)

Speakjava
Speakjava

Reputation: 3400

The answer is, you can't.

If you dig into the JVM source code, you'll find that the logging framework will attempt to open the file name you provide using an fopen library call. If the log file does not exist, it is created (the file open mode is append). However, the logging code does not make any attempt to traverse the path provided and create directories where they do not exist.

This isn't unreasonable behaviour on the part of the JVM.

If you really want to do this, you need a script that will ensure the directory exists before running your application.

Upvotes: 5

Related Questions