执行命令在cmd中工作,而不是在java代码中工作

时间:2016-03-14 21:58:40

标签: java cloud

我对此代码有疑问 我使用CLC(云资源)来将应用程序迁移到云端 我的代码是

public class cmd1 {

public static void main(String[] args) throws IOException, InterruptedException {
    String[] command =
        {
            "cmd",
        };
        Process p = Runtime.getRuntime().exec(command);
        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
        new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());
        stdin.println("dir c:\\ /A /Q");  //ca march bien 


        stdin.println("cf login");        //ca march    

        stdin.println("hakimguettaoui@gmail.com");   //ici si j'ai fait un login il me demander Email et mot de pass mais il me 
                                                     // donnée un pb 
        stdin.println("");


        // write any other commands you want here
        stdin.close();
        int returnCode = p.waitFor();
        System.out.println("Return code = " + returnCode);

}

}

class SyncPipe implements Runnable {


public SyncPipe(InputStream istrm, OutputStream ostrm) {
 istrm_ = istrm;
 ostrm_ = ostrm;}
 public void run() {
 try
 {
     final byte[] buffer = new byte[1024];
     for (int length = 0; (length = istrm_.read(buffer)) != -1; )
     {
         ostrm_.write(buffer, 0, length);
     }
 }catch (Exception e)
 {
     e.printStackTrace();
 } } private final OutputStream ostrm_; private final InputStream istrm_;

}

所有命令都有效但是 如果我使用cf登录是工作但是认证 是不是工作锁定错误 enter image description here

1 个答案:

答案 0 :(得分:0)

您正确地执行此操作,为STDOUT和STDERR分隔线程。使用Runtime多年后,我切换到ProcessBuilder ......也许切换到ProcessBuilder。请参阅以下帖子:

Java Runtime OR Processbuilder OR Other

Java - ProcessBuilder command arguments with spaces and double-quotes fails