Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, it is possible.</p> <p>At the end of my answer is an example program. It is far from perfect and is missing some implementation details. For example proper exception handling and also detect when the cmd has exited... But it may be used as a starting point.</p> <p>In essence the solution to your question is to start cmd.exe as a new Process. Then read commands in java from standard input stream (System.in) and pipe it to the cmd.exe-process. To provide feedback to the user you must read the standard output from cmd.exe-process and print it to the standard output of your java process (System.out). Also read standard error from cmd.exe-process and print it to standard error of your java process (System.err).</p> <p>Close all resources when you are done. I indicated this in the example, but this is not production ready. Any exception would prevent the example program from cleaning up.</p> <p>Another implementation detail: The example program uses a second thread to read output from cmd.exe-process. Otherwise you will only get output when the user hits enter.</p> <p>Finally, here is the code:</p> <pre><code>package com.example; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class JavaCmd { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { ProcessBuilder procBuilder = new ProcessBuilder("cmd.exe"); Process proc = procBuilder.start(); PrintWriter outToProc = new PrintWriter(proc.getOutputStream()); final BufferedReader inFromProc = new BufferedReader(new InputStreamReader(proc.getInputStream())); final BufferedReader errorFromProc = new BufferedReader(new InputStreamReader(proc.getErrorStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Thread outputThread = new Thread(new Runnable(){ @Override public void run() { while(true) { try { while(inFromProc.ready()) { String line = inFromProc.readLine(); System.out.println(line); } while(errorFromProc.ready()) { String line = errorFromProc.readLine(); System.err.println(line); } } catch (IOException e) { throw new RuntimeException("Error in output thread", e); } try { Thread.sleep(100); } catch(InterruptedException e) { System.out.println("Output Thread interrupted -&gt; Thread will terminate"); break; } } } }); outputThread.start(); System.out.println("\n\nProxy shell is ready. Enter 'quit' to leave program.\n\n"); System.out.flush(); String line = null; while((line = reader.readLine()) != null) { if(line.equalsIgnoreCase("quit")) { System.out.println("Good Bye"); break; } outToProc.println(line); outToProc.flush(); } reader.close(); outputThread.interrupt(); proc.destroy(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload