1. 程式人生 > 其它 >java程式碼連線linux後,執行linux命令

java程式碼連線linux後,執行linux命令

我需要執行的linux命令是:

hdfs dfs -put [本地目錄] [hadoop目錄]

  把jar包複製到hdfs某個資料夾下,

匯入的jar包,

import com.jcraft.jsch.ChannelExec;

  新建空物件:

  ChannelExec openChannel =null;

  在連線關閉前:

     //把jar包放到hdfs目錄
        openChannel = (ChannelExec) session.openChannel("exec");
        openChannel.setCommand("hdfs dfs -put "+remotePath+localFile+"  .hiveJars");//括號內放的是linux命令
        int exitStatus = openChannel.getExitStatus();
        openChannel.connect();
 openChannel.setCommand("需要執行的linux命令");


------------------------------------------------------------------------------------------------------------------------------------



下面是在網上找到的別的方法 :(針對於我的需求失敗)
Runtime.getRuntime().exec("sh generator.sh").waitFor();

  一直報找不到指定檔案,網上找了很多方法也不行,這是在網上找的解決方案(沒能解決我的問題)附連結:https://www.cnblogs.com/zhanzhuang/p/9304502.html。