2

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

kentcdodds
  • 27,113
  • 32
  • 108
  • 187
  • Check out this question doing a very similar thing: http://stackoverflow.com/questions/2460297/run-shell-command-from-java – Karl Barker Apr 18 '12 at 19:00
  • Sorry, it looks like that's for input from the user. I'm only interested in output. See update note. – kentcdodds Apr 18 '12 at 19:03

3 Answers3

1

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

Community
  • 1
  • 1
Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • No, it's not for receiving input. It's only for viewing output. See updated note. – kentcdodds Apr 18 '12 at 19:01
  • You could also display the output via another panel.Why present a command line?From my point of view even printing the output to a file and informing the user via a message box that output has been completed seems more natural than presenting a command line.Unless I am wrong, I have seen this aproach only in installation wizards and it is for specific reasons – Cratylus Apr 18 '12 at 19:04
  • 1
    I may end up doing it that way. It'd just be really easy to show it in command prompt because that's where `System.out.println();` is going anyway, rather than re-directing all my messages to a panel or file. – kentcdodds Apr 18 '12 at 19:06
  • 1
    Well you are wrong here!You can redirect `System.out` to a file. http://stackoverflow.com/questions/2851234/system-out-to-a-file-in-java – Cratylus Apr 18 '12 at 19:09
  • Ha ha, you're right, and I was misleading in my comment. It would be easier for the user to see it as it's going. It's kind of a debugging feature of my program. Whether that's a better way to do it or not is irrelevant to this question. My question is all about how to open a command prompt window and show the output of the program after runtime. Thanks for the tip though! – kentcdodds Apr 18 '12 at 19:11
  • Ok,but I already provided a link for you to see how to do that.If you look at that post, the answers have many examples on how to open a command prompt and accept input.So it fits your needs – Cratylus Apr 18 '12 at 19:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10241/discussion-between-kentcdodds-and-user384706) – kentcdodds Apr 18 '12 at 19:16
1

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.

Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
0

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

Colin D
  • 5,641
  • 1
  • 23
  • 35