Is it possible to run cmd through a java program -


this question has answer here:

i know if possible run cmd through java. not 1 command @ time continuous stream of user input commands relays info received. possible or should stop trying now?

i'm not sure why i'm attaching this; it's not relevant, trying accomplish with. however, resets cmd after every command. (yes, realize bad coding, i'm attempting boss asked about.)

import java.io.*; import java.util.scanner;  public class cmd {      public static void main(string[] args) throws ioexception {          string line;          while (true) {             scanner scanner = new scanner(system.in);             string comm = scanner.nextline();             processbuilder builder = new processbuilder("cmd.exe", "/c", comm);             builder.redirecterrorstream(true);             process p = builder.start();             bufferedreader r = new bufferedreader(new inputstreamreader(p.getinputstream()));              while (true) {             line = r.readline();             if(line==null){break;}                 system.out.println(line);            }        }    } } 

basically, cmd behind java gui user input commands end game. if tell me if possible , if point me in right direction grateful.

yes, possible.

at end of answer example program. far perfect , missing implementation details. example proper exception handling , detect when cmd has exited... may used starting point.

in essence solution question start cmd.exe new process. read commands in java standard input stream (system.in) , pipe cmd.exe-process. provide feedback user must read standard output cmd.exe-process , print standard output of java process (system.out). read standard error cmd.exe-process , print standard error of java process (system.err).

close resources when done. indicated in example, not production ready. exception prevent example program cleaning up.

another implementation detail: example program uses second thread read output cmd.exe-process. otherwise output when user hits enter.

finally, here 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 -> thread terminate");                         break;                     }                 }             }         });          outputthread.start();         system.out.println("\n\nproxy shell ready. enter 'quit' 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();     }  } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -