Created
December 7, 2012 09:54
-
-
Save jpospychala/4232222 to your computer and use it in GitHub Desktop.
java terminal for windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testswt; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.custom.StyledText; | |
import org.eclipse.swt.events.KeyEvent; | |
import org.eclipse.swt.events.KeyListener; | |
import org.eclipse.swt.layout.GridData; | |
import org.eclipse.swt.layout.GridLayout; | |
import org.eclipse.swt.widgets.Display; | |
import org.eclipse.swt.widgets.Shell; | |
public class MainApp { | |
/** | |
* @param args | |
* @throws IOException | |
*/ | |
public static void main(String[] args) throws IOException { | |
Display display = new Display (); | |
Shell shell = new Shell(display); | |
final StyledText st = new StyledText(shell, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL); | |
shell.setLayout(new GridLayout(1, true)); | |
st.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
shell.open (); | |
ProcessBuilder pb = new ProcessBuilder(); | |
//pb.redirectInput(Redirect.INHERIT); | |
final Process process = pb.command("cmd.exe").start(); | |
Thread pOut = new Thread(new Runnable() { | |
byte[] buf = new byte[4096]; | |
int len; | |
@Override | |
public void run() { | |
InputStream processOut = process.getInputStream(); | |
try { | |
while ((len = processOut.read(buf)) != -1) { | |
Display.getDefault().syncExec(new Runnable() { | |
@Override | |
public void run() { | |
st.append(new String(buf, 0, len)); | |
st.setCaretOffset(st.getCharCount()); | |
st.showSelection(); | |
} | |
}); | |
} | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println("read DONEE"); | |
} | |
}); | |
pOut.start(); | |
Thread pErr = new Thread(new Runnable() { | |
byte[] buf = new byte[4096]; | |
int len; | |
@Override | |
public void run() { | |
InputStream processOut = process.getErrorStream(); | |
try { | |
while ((len = processOut.read(buf)) != -1) { | |
Display.getDefault().syncExec(new Runnable() { | |
@Override | |
public void run() { | |
st.append(new String(buf, 0, len)); | |
st.setCaretOffset(st.getCharCount()); | |
st.showSelection(); | |
} | |
}); | |
} | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println("read DONEE"); | |
} | |
}); | |
pErr.start(); | |
final OutputStream os = process.getOutputStream(); | |
st.addKeyListener(new KeyListener() { | |
@Override | |
public void keyReleased(KeyEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
if (e.character < 9) { | |
return; | |
} | |
System.out.println(e.character); | |
System.out.flush(); | |
try { | |
os.write(e.character); | |
if (e.character == '\r') { | |
os.write('\n'); | |
} else { | |
} | |
os.flush(); | |
} catch (IOException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} | |
}); | |
while (!shell.isDisposed ()) { | |
if (!display.readAndDispatch ()) display.sleep (); | |
} | |
display.dispose (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment