1

English : Is it possible in java to know the output files specified when executing a jar (without parsing parameters) ? For example with this command :

java -jar application.jar 1> output.log 2> error.err

Java can know the default output is in output.log and error in error.err ?

Français : Est-il possible en java de connaitre les fichiers de sorties spécifiés lors de l'éxécution d'un jar (sans parser les arguments) ?

Par exemple dans le cas de l'éxecution de cette commande :

java -jar application.jar 1> output.log 2> error.err 
Skaÿ
  • 326
  • 1
  • 3
  • 15
  • 1
    do you want to know the name of output.log and error.err file names in your Main() method?? – PyThon Jan 12 '17 at 11:48
  • The only ways I can think of are all platform-specific and/or involve JNI. And even those can be unreliable. For starters, see http://stackoverflow.com/questions/11221186/how-do-i-find-a-filename-given-a-file-pointer – Andrew Henle Jan 12 '17 at 12:08

1 Answers1

3

The way you are redirecting output to files is done at operating system level but not on java level, so its not possible the way you are doing.

[Alernate way]

The only way it is possible if you explicitly send the the file name as java arg to the program and change the system output stream manually.

Run Command

java -jar application.jar output.log error.err

Main Class

public static void main(String[] args){
  // file names will be available as args[1] and args2[]
  String outputFileName = args[1];
  String errorOutputFileName = args[2];
  System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(args[1]))));
  System.setErr(new PrintStream(new BufferedOutputStream(new FileOutputStream(args[2]))));
  // ...

}
PyThon
  • 1,007
  • 9
  • 22