0

I have a problem with crashed Java Virtual Mashines without error message. I often do some checks in my code. Something went wrong, if one of these checks will not be fulfilled and the java program shall not carry on. E.G.:

if(!constraint){
   System.err.println("The constraint is not fullfilled!");
   System.exit(-1);
}

Can I be sure, that an error message with System.err.println("...") is printed on the console before the JVM is killed with System.exit(-1)? If not, this could explain why my JVM crashes without error message.

UweJ
  • 447
  • 3
  • 10
  • The console is not part of the JVM. Therefor it is possible that in some environments your message will not get printed, but I am not aware of any such environments. –  Aug 08 '20 at 17:54
  • I run the java program on a NixOS (Linux) server:Operating System: NixOS 18.09.2574.a7e559a5504 (Jellyfish), Kernel: Linux 4.14.118, Architecture: x86-64 – UweJ Aug 08 '20 at 18:06
  • The JVM is a oracle one: java version "12.0.2" 2019-07-16, Java(TM) SE Runtime Environment (build 12.0.2+10), Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing) – UweJ Aug 08 '20 at 18:06
  • The java code is build by an ant script and run by: ant -Dstring_prompt="$PATHPREFIX/sim_1/config.txt" "SimulationModel" > $PATHPREFIX/sim_1/commandlineOutput.txt & – UweJ Aug 08 '20 at 18:06
  • The java program needs a configuration file ($PATHPREFIX/sim_1/config.txt) and forwards the commandline outputs into another file (PATHPREFIX/sim_1/commandlineOutput.txt). – UweJ Aug 08 '20 at 18:06
  • The answer by Danilo Sales is imho the most likely source of your problems. –  Aug 08 '20 at 18:37

1 Answers1

1

System.out leads the output to the standard output stream (normally mapped to the console screen). System.err leads the output to the standard error stream (and, by default to the console). The idea behind having these two is that the standard output should be used for regular program output, and standard error should be used for error messages. Depend how to load a java program the error stream is not printed on console.

Both the streams can be redirected to different destinations. If it is desired to redirect the output to an output file and error messages to a different log file, than on UNIX it can be done as follows:

java MyClass > output.log 2>error.log

This causes the regular output (using System.out) to be stored in output.log and error messages (using System.err) to be stored in error.log.

Another option is in your code redirect the outputs:

PrintStream outStream = new PrintStream(new File("outFile.txt"));
System.setOut(outStream);
System.out.println("This is a baeldung article");

And in case of System.err:

PrintStream errStream = new PrintStream(new File("errFile.txt"));
System.setErr(errStream);
System.err.println("This is a baeldung article error");

The System.out and System.err is not a better solution to check this, I suggest to add a logger like SL4J to log this in appropriate file or output.

Danilo Sales
  • 46
  • 1
  • 4
  • 3
    In bash, you can also use ` > output.log 2>&1` to redirect stderr to stdout, and then redirect stdout to output.log. –  Aug 08 '20 at 18:22
  • But can I be sure, that the error message is flushed/printed before the Sytem.exit(-1) hits which will suppress the message flush? – UweJ Aug 08 '20 at 20:31
  • 1
    Yes, the message is flushed before exit system. – Danilo Sales Aug 11 '20 at 10:54