我正在尝试从Android手机向raspi发送连续命令。我使用了此链接中的代码,但出现错误。

链接:https://stackoverflow.com/questions/23471439/android-sshexecute-multiple-commands-with-sshj/23806942?noredirect=1#comment36621476_23806942

主要:

package com.example.sshjdeneme;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buton1 = (Button) findViewById(R.id.button1);
    buton1.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Connection conn = new Connection();
    conn.execute("sudo uvccapture -m");

}

}


连接类别:

        package com.example.sshjdeneme;

        import java.io.IOException;

        import net.schmizz.sshj.*;
        import net.schmizz.sshj.connection.channel.direct.Session;
        import net.schmizz.sshj.connection.channel.direct.Session.Command;
        import net.schmizz.sshj.transport.TransportException;
        import net.schmizz.sshj.userauth.UserAuthException;
        import android.os.AsyncTask;
        import android.util.Log;

        public class Connection extends AsyncTask<String, String, String> {
            /**
        * @param args
        * @throws TransportException
        * @throws UserAuthException
        */
          final SSHClient ssh = new SSHClient();
          int pn = 22;
           String ipaddress = "192.168.1.1";
           String username = "pi";//root
           String password = "berkfurkan";//sah
           String result1 ="" ;
           String result2 ="" ;
           String result ="";

        @Override
        protected String doInBackground(String... command) {


          Log.i("doInBackground","doInBackground");
        //final SSHClient ssh = new SSHClient();
          String command1= new String(command[0]);
               // Adds a nullHostKeyVerifier
          ssh.addHostKeyVerifier(new NullHostKeyVerifier());
          try {
              ssh.connect(ipaddress, pn);

          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
              return "NOT connecting";
          }

          // Authenticate with the password entered
                  try {
                      ssh.authPassword(username, password);
                  } catch (UserAuthException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                      return "NOT Authenticate";

                  }catch (TransportException e) {
                    // TODO: handle exception
                      e.printStackTrace();
                      return "NOT Authenticate";
                }



           // connect to the machine
           try {

               // start a new session
              Session session = ssh.startSession();
              Command cmd = session.exec(command1);



              if (cmd.isOpen())
                  {System.out.println("channel oppen");
                  System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
                  System.out.println("\n** exit status: " + cmd.getExitStatus());}



              System.out.println("output"+IOUtils.readFully(cmd.getInputStream()).toString());
               // reads the output of the command
           result = IOUtils.readFully(cmd.getInputStream()).toString();

                session.close();

           } catch (IOException e) {
               e.printStackTrace();
               return "session" ;

           }

           return result;

            }
        public void SSH() {
          try {
          ssh.addHostKeyVerifier(new NullHostKeyVerifier());
          ssh.connect(ipaddress, pn);
          ssh.authPassword(username, password);
        } catch (IOException e) {
          System.out.println(e.getMessage());
        }
        }

        @Override
        protected void onPostExecute(String result) {
          if(result.equalsIgnoreCase("NOT connecting")){
               Log.i("res1","NOT connecting");
              }
          if(result.equalsIgnoreCase("NOT Authenticate")){
               Log.i("res2","NOT Authenticate");
              }
          if(result.equalsIgnoreCase("session")){
               Log.i("res3","NOT session");
              }
          if (result.equals(""))
          Log.i(" resultat","NULL");
          else
              Log.i(" resultat",result);

        }
        }


空主机类:
        包com.example.sshjdeneme;

    import java.security.PublicKey;

    import net.schmizz.sshj.transport.verification.HostKeyVerifier;

    public class NullHostKeyVerifier implements HostKeyVerifier {

    /*
    * This method is used to bypass HostKeyVerification.
    * It returns true for whatever the input is.
    *
    */


    @Override
    public boolean verify(String arg0, int arg1, PublicKey arg2) {
        // TODO Auto-generated method stub
        return false;
    }
    }


我在哪里做错了?谢谢

ps:错误消息

05-22 12:22:10.575: E/AndroidRuntime(379): FATAL EXCEPTION: main
05-22 12:22:10.575: E/AndroidRuntime(379): java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
05-22 12:22:10.575: E/AndroidRuntime(379):  at net.schmizz.sshj.DefaultConfig.<init>(DefaultConfig.java:97)
05-22 12:22:10.575: E/AndroidRuntime(379):  at net.schmizz.sshj.SSHClient.<init>(SSHClient.java:136)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.example.sshjdeneme.Connection.<init>(Connection.java:20)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.example.sshjdeneme.MainActivity.onClick(MainActivity.java:31)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.view.View.performClick(View.java:2485)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.view.View$PerformClick.run(View.java:9080)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.os.Handler.handleCallback(Handler.java:587)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.os.Looper.loop(Looper.java:123)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-22 12:22:10.575: E/AndroidRuntime(379):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 12:22:10.575: E/AndroidRuntime(379):  at java.lang.reflect.Method.invoke(Method.java:507)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-22 12:22:10.575: E/AndroidRuntime(379):  at dalvik.system.NativeStart.main(Native Method)

最佳答案

根据所有在堆栈跟踪中指出的异常情况,非常确定它只是类路径中缺少的库:

造成原因:java.lang.NoClassDefFoundError:org.slf4j.LoggerFactory

sshj依赖slf4j库进行日志记录,我假设您只需要下载它并将其放在类路径中即可。

slf4j可从以下地址获得:http://www.slf4j.org/download.html

您可能希望检查sshj还需要哪些其他依赖项,并确保也安装了它们,不确定所使用的sshj版本是什么,但是可以找到最新版本的pom.xml链接,其中列出了所有下面sshj的当前依赖项:
https://github.com/hierynomus/sshj/blob/master/pom.xml

希望这可以帮助!

07-27 23:14