我一直在通过Java处理命令执行器。我想知道我是否可以不止一次循环,如果是这样,怎么样?示例在Sudo Code中的步骤:
抱歉我的清晰度差。
这是我的代码:
import java.lang.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
public class Dos implements Runnable
{
private PipedOutputStream pipeout;
private PipedOutputStream pipeerr;
String command = "cmd /K ";
public Dos ( String[] cmd )
{
pipeout = null;
pipeerr = null;
}
@Override
public void run ()
{
exec ();
}
public void exec ()
{
// Class to redirect the process input stream to a piped output stream
class OutputMonitor implements Runnable
{
InputStream is;
PipedOutputStream pout;
public OutputMonitor ( InputStream i, PipedOutputStream p )
{
is = i;
pout = p;
}
public void run ()
{
try
{
int inputChar;
for ( ;; )
{
inputChar = is.read();
if ( inputChar == -1 )
{ break; }
if ( pout == null )
{
System.out.write ( inputChar );
}
else
{
pout.write ( inputChar );
}
}
if ( pout != null )
{
pout.flush ();
pout.close ();
}
else
{
System.out.flush();
}
}
catch ( Exception e ) { e.printStackTrace (); }
}
}
try
{
Runtime r = Runtime.getRuntime ();
JOptionPane.showConfirmDialog(null, "(uses quotes for system Commands eg: \"java -jar BuildTools.jar --Latest \" )");
String cmdargs = JOptionPane.showInputDialog("Type in Command:", "");
Process p = r.exec (command + cmdargs);
OutputMonitor out = new OutputMonitor ( p.getInputStream (), pipeout );
OutputMonitor err = new OutputMonitor ( p.getErrorStream (), pipeerr );
Thread t1 = new Thread ( out );
Thread t2 = new Thread ( err );
t1.start ();
t2.start ();
}
catch ( Exception e ) { e.printStackTrace (); }
}
public PipedInputStream getInputStream () throws IOException
{
pipeout = new PipedOutputStream ();
return new PipedInputStream ( pipeout );
}
public PipedInputStream getErrorStream () throws IOException
{
pipeerr = new PipedOutputStream ();
return new PipedInputStream ( pipeerr );
}
public void execInThread ()
{
Thread t = new Thread ( this );
t.start ();
}
public static JPanel getContentPane ( JTextArea ta )
{
JPanel p = new JPanel ( new BorderLayout () );
JPanel bottom = new JPanel ( new FlowLayout () );
JButton button = new JButton ( " Exit " );
button.addActionListener ( new ActionListener ( )
{
public void actionPerformed ( ActionEvent e )
{
System.exit ( 0 );
}
}
);
bottom.add ( button );
p.add ( new JScrollPane ( ta ), BorderLayout.CENTER );
p.add ( bottom, BorderLayout.SOUTH );
p.setPreferredSize ( new Dimension ( 640,480 ) );
return p;
}
public static void main ( String[] arg )
{
class GuiUpdate implements Runnable
{
private PipedInputStream pin;
private PipedInputStream perr;
private JTextArea outputArea;
GuiUpdate ( JTextArea textArea, PipedInputStream in )
{
pin = in;
outputArea = textArea;
}
@Override
public void run ()
{
// Class to run on the event dispatch thread to update the GUI
class UpdateSwing implements Runnable
{
String update;
JTextArea swingTextArea;
public UpdateSwing ( JTextArea a, String s )
{
update = s;
swingTextArea = a;
}
public void run ()
{
outputArea.append ( update + "\n" );
}
}
try
{
// Read file before displaying
BufferedReader r = new BufferedReader ( new InputStreamReader ( pin ) );
String line;
for ( ;; )
{
line = r.readLine ();
if ( line == null ) { break; }
SwingUtilities.invokeLater ( new UpdateSwing ( outputArea, line ) );
Thread.yield ();
}
}
catch ( Exception e ) { e.printStackTrace (); }
}
}
// Create and invoke GUI
JFrame f = new JFrame ( "Minecraft GUI" );
f.setDefaultCloseOperation ( JFrame.ICONIFIED );
JTextArea textOutput = new JTextArea ();
textOutput.setBackground(Color.BLACK);
textOutput.setForeground(Color.red);
f.getContentPane().add ( getContentPane ( textOutput ) );
JButton b = new JButton("Do Command");
f.pack();
f.show ();
try
{
// Create command and setup the pipes
Dos d = new Dos ( arg );
PipedInputStream stdout_pipe = d.getInputStream ();
PipedInputStream stderr_pipe = d.getErrorStream ();
d.execInThread ( );
// Results
Thread t1 = new Thread ( new GuiUpdate ( textOutput, stdout_pipe ) );
Thread t2 = new Thread ( new GuiUpdate ( textOutput, stderr_pipe ) );
t1.start ();
t2.start ();
}
catch ( Exception e ) { e.printStackTrace (); }
}
}