【问题标题】:Execute Command work in cmd and not work in java code执行命令在 cmd 中工作而不在 java 代码中工作
【发布时间】:2016-03-15 09:55:42
【问题描述】:

我对这段代码有疑问 我使用 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

【问题讨论】:

    标签: java cloud


    【解决方案1】:

    您做得对,将 STDOUT 和 STDERR 的线程分开。使用 Runtime 多年后,我切换到 ProcessBuilder...也许切换到 ProcessBuilder。请参阅以下帖子:

    Java Runtime OR Processbuilder OR Other

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      相关资源
      最近更新 更多