在Android上打开SSH连接

时间:2014-12-26 16:20:34

标签: java android ssh jsch

我正在尝试在Android上打开SSH连接,但每当我调用该方法来执行SSH命令时,应用程序崩溃。我在MainActivity中调用方法如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new AsyncTask<Integer, Void, Void>(){
               @Override
               protected Void doInBackground(Integer... params) {
                   try {
                         SSH.openSSH();
                   } catch (Exception e) {
                         e.printStackTrace();
                   }
                   return null;
               }
            }.execute(1);
    }

SSH.openSSH()包含以下内容:

public static void openSSH() throws JSchException, IOException
    {
        JSch jsch = new JSch();

        jsch.setKnownHosts("known_hosts.txt");

        Session session = jsch.getSession(username, hostname, port);
        session.setPassword(password);
        session.connect();

        ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

        String rawData = getData(channelExec);
        String[] data = parseData(rawData);
        setData(data);

        channelExec.disconnect();
        session.disconnect();
    }

但我得到的错误如下:

12-26 18:07:54.606: E/AndroidRuntime(29704): FATAL EXCEPTION: AsyncTask #1
12-26 18:07:54.606: E/AndroidRuntime(29704): java.lang.RuntimeException: An error occured while executing doInBackground()
12-26 18:07:54.606: E/AndroidRuntime(29704):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.lang.Thread.run(Thread.java:856)
12-26 18:07:54.606: E/AndroidRuntime(29704): Caused by: java.lang.NoClassDefFoundError: com.jcraft.jsch.JSch
12-26 18:07:54.606: E/AndroidRuntime(29704):    at com.example.raspberrypi.SSH.openSSH(SSH.java:26)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at com.example.raspberrypi.MainActivity$1.doInBackground(MainActivity.java:19)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at com.example.raspberrypi.MainActivity$1.doInBackground(MainActivity.java:1)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-26 18:07:54.606: E/AndroidRuntime(29704):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-26 18:07:54.606: E/AndroidRuntime(29704):    ... 4 more

有人能向我解释我做错了吗?

0 个答案:

没有答案