skip to Main Content

I have a program where, during a long task, I use System.out to print messages to indicate its progress. However, this can only be seen in the terminal in Visual Studio Code.

When I export the program to a jar file and double-click it, it doesn’t print out any System.out messages, although it is still able to do its tasks.

So, how do I make my program open a terminal upon double-clicking its jar file so that it can print messages to it in a similar manner as using System.out?

2

Answers


  1. Try running with java cli. This is basically what your IDE does when you run your project.

    java -jar yourjar.jar
    

    If you don’t want to keep your console open and still persist logs, I would suggest using log4j or just simply redirect output of System.out to file

    Login or Signup to reply.
  2. You can easily change where stderr and stdout go:

    public class RedirectStdStreams {
        public static void main(String[] args) throws Exception {
            System.setOut( new PrintStream( "/tmp/stdout" ) );
            System.setErr( new PrintStream( "/tmp/stderr" ) );
    
            System.out.println( "Look ma, watch me juggle this chainsaw!" );
            System.err.println( "Uh oh, that didn't go well at all...");
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search