kentcdodds
kentcdodds

Reputation: 29081

Programmatically show command prompt/terminal output

I wasn't sure how to search for this. I have a swing application. I want to allow the user to open the command prompt and see the output (System.out.println("MY_OUTPUT");) as they go through the application. However, in most cases this wont be necessary, so it would be an option they can enable after starting the application. Is there any way to do this? Basically, I'm looking for some way to open command prompt or the terminal associated with my program at will at any time. Thanks for the help.

Note1: This has nothing to do with receiving input from the user, it's all about output to the user.

Note2: I realize that I could (and maybe should) do this in a separate swing window with a text area, but everything I have as it is is going to System.out.println();, and I want to use this same kind of thing in future projects I think, so I would really like to be able to do this using command prompt. Besides, that's my question in the first place. Thanks :D

Upvotes: 2

Views: 5586

Answers (3)

Jesse Webb
Jesse Webb

Reputation: 45303

I imagine this requirement is coming from you using a simple logging strategy in your app by just using System.println() statements. You should wrap you System.println() method calls in some kind of logger pattern class. You can create a Logger interface with a Log method. The ConsoleLogger impl would just write to the console and would be useful for you in testing/debugging.

Then for your real users, you have a couple options. You could use a FileLogger and make them read log files if they want to see output of your app. Or you could make some kind of CustomTextWindowLogger that logs to some GUI window, or maybe still a file that a GUI window could display data from.

Either way, I have never seen an app redirect System.println() statements to a graphical component because it it just sends data to the standard output stream. Along those line of thinking, I guess you could also trying changing the System.out output stream to a different stream which your GUI window can display. This would be easier to implement quickly but would be harder to change down the line. With the Logger interface, you can change your mind easily about how the output is given to the user.

Upvotes: 1

Cratylus
Cratylus

Reputation: 54094

To be honest I find it odd in a GUI environment to accept input from the user via command line instead of some dialog (I mean you display a GUI) but you could just use a Runtime to execute a command prompt. Look here cmd using java

Upvotes: 1

Colin D
Colin D

Reputation: 5661

Why not just pop up a textfield/area (or whatever is in Swing), and write the messages there?

Upvotes: 0

Related Questions