Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: How to redirect System.out to command line window

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default How to redirect System.out to command line window

    Hi.
    In my application I open a command line window with the following code:
    	try {
    		Runtime.getRuntime().exec(new String[] {"cmd.exe", "/C", "\"start; cd c:\\\""});
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    This works fine.
    However, I would like to redirect System.in and System.out to this newly created window. How could I do that?
    This is supposed to work for java applications which have been started with a double click (executable jar).

    Thank you in advance!


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    As I'm running Linux at the moment, I can't test anything. That said, my assumption would be to try something like:

    public static void main(String args[]) {
    	final String[] params = {"cmd.exe", "/C", "\"start; cd c:\\\""};
     
    	try {
    		Process process = Runtime.getRuntime().exec(params);
    		System.setOut(process.getOutputStream());
    		System.setIn(process.getInputStream());
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }

    As it should be obvious from my opening statement however, this sort of Java code will no longer be interoperable between all operating systems.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    Thanks for the quick response. I tried that already and it didnt work. I couldnt read any input and outputs where not displayed in the command prompt. But thanks anyways.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    Okay, how about .setOut(new PrintStream(getStream))?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    Did that too, since the "setOut" method needs a printStream. You can not just set it to the output stream returned by process.getOutputStream().

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to redirect System.out to command line window

    But process is just a Process object, isn't it? Which would mean that it does not necessarily "know" a console window exists. I could be wrong, but I believe the process.getOutputStream() and process.getInputStream() methods get the input and output streams of the Process itself, not of the console the process created... In fact, I don't even know if you can get the console that was created.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    Quote Originally Posted by aussiemcgr View Post
    [...] In fact, I don't even know if you can get the console that was created.
    Well, I dont know either. But if there is a way I would sure like to know.

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    While the Process is alive, the hooks to both streams do refer to opened process. In the past, I've used the input streams to read values returned by .exe files.

    --- Update ---

    Quote Originally Posted by Cornix View Post
    Did that too, since the "setOut" method needs a printStream. You can not just set it to the output stream returned by process.getOutputStream().
    setOut(process.getOutputStream()) would work directly, but my thinking was to test it while it was wrapped in a new stream. But obviously that did not work either.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    Quote Originally Posted by newbie View Post
    setOut(process.getOutputStream()) would work directly, but my thinking was to test it while it was wrapped in a new stream. But obviously that did not work either.
    When I try that my IDE (Eclipse) gives me the following error:
    The method setOut(PrintStream) in the type System is not applicable for the arguments (OutputStream)
    So I am pretty sure it wouldnt work that way.

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    Fair doos.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to redirect System.out to command line window

    Two things:
    1. I believe the streams coming from the Process object are not ones you can add to, so you would not be able to append the System.out to the stream
    2. The "output" (meaning what a command line would print out while running a command) for a Process object is actually obtained with Process.getInputStream(). Quoted from the api:
    public abstract OutputStream getOutputStream()
    ...

    Returns:
    the output stream connected to the normal input of the subprocess.

    ...
    public abstract InputStream getInputStream()
    ...
    Returns:
    the input stream connected to the normal output of the subprocess.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #12
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    But I would like to make my output to the input of the process.
    Like:
    System.setOut(process.getInputStream());
    With the intention that any further print commands would print to the command prompt. How would I do that?

    And, if this is not possible, does anybody here have a better idea how to add a simple output to my application without having to start the application via the command prompt? I know I could output to a text file, but that wouldnt update in real time as new output comes in while the text file is opened in some kind of editor.

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to redirect System.out to command line window

    Ok. So your entire goal here is to just make a console window pop up when you double click the jar, correct?
    I'm not sure if you can do that without requiring the user to modify the default executing command for the jar (jars, by default, run without the command prompt). I would think there would HAVE to be a way to create a Console object, but I have no idea how.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  14. #14
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    I've got a weird but plausable approach for you! I found one of my earliest Swing applications from University a few years back which simulates a command prompt in Swing. Its appearance can be seen in the picture below, and although it has very limited functionality and is probably built on a terrible code base, it could work
    WXUXtlt.jpg

    All the extra code you would need is:
    	public static void main(String[] args) {
    		ConsoleWindow console = new ConsoleWindow();
    		console.setVisible(true);
    		JScanner scanner = console.getScanner();
     
    		System.out.println("Please Enter Your Name: ");
    		scanner.nextLine();
    	}

    where you use the JScanner instead of a traditional Scanner. All functions for the JScanner are replicated, so its API is practically identical if i remember rightly.
    If you do fancy trying it out, heres the jar: https://www.dropbox.com/s/x7pfxsgrks...ingConsole.jar
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  15. #15
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to redirect System.out to command line window

    Did you already attach the System.in and out streams to the console?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  16. #16
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    If that was directed at me, i think it was done using System.setOut(new PrintStream()) etc but using PipedOutputStream and an extra thread. The inputstream element was never added, as it simply comes from the JTextField.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  17. #17
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    A custom implementation of the command line would of course always be another option. But I would have really appreciated if there was some simple way. Its funny that this is such a difficult problem for java; I would not have guessed that.

  18. #18
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to redirect System.out to command line window

    Well, the "simple" way is to change your run configurations for that specific jar, but that has to be independently done by each user on their individual operations systems, which no one wants to try to explain to an end-user...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  19. #19
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    Quote Originally Posted by aussiemcgr View Post
    [...] no one wants to try to explain to an end-user...
    Exactly that is the problem why I want that command prompt to open in the first place. Try to tell somebody who is not tech-savvy how to start a java application via command prompt if that java application needs native libraries.

  20. #20
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to redirect System.out to command line window

    The issue stems from there being two different executable vms for java. One of them: javaw.exe (I think this one) does not have a console. The other one: java.exe does have a console. By default, jars are assumed to be "window apps", so they run with javaw.exe. You can right-click the jar, go to run-as, and find the java.exe one, and it should open a console, but 99% of users would never do that (they would just double-click).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  21. #21
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to redirect System.out to command line window

    I dont seem to be able to start it with console via run-as. In the drop-down menu I have "Java(TM) Platform SE Binary"; when I click that, nothing happens. The application does not start.
    Double clicking works though, and it also works to start the application via command prompt.

  22. #22
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to redirect System.out to command line window

    If you just wanted an easier way for end users to run your app without typing into cmd (instead of needing 2 windows), just ship an executable batch file with your jar.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. sdtin/command-line
    By TorontoJava1984 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 16th, 2011, 01:51 PM
  2. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  3. [SOLVED] Redirect command prompt output to log file.
    By goldest in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 24th, 2010, 05:26 AM
  4. How To Make JCreator Output in a Command Window
    By Kimimaru in forum Java Theory & Questions
    Replies: 4
    Last Post: October 4th, 2010, 09:36 PM
  5. [SOLVED] Command Line Argument Help
    By EmSaint in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 10:55 AM