2

我正在尝试在 android 中创建一个小型应用程序,以使用 jsch 库通过 ssh 运行命令。没有问题出在哪里,但是当我运行应用程序时,我得到一个窗口:

不幸的是,应用程序已停止。

这是我的代码:

安卓清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xxxxxxxx"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> 
    </application>
</manifest>

主要活动:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);     


    TextView tvBd = (TextView) findViewById(R.id.TxtNombre);    

    ConexionSSH conect = new ConexionSSH();
    String prueba = "pepe";     

    try {
        prueba = conect.conect();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    tvBd.setText("LS: "+prueba);

连接类:

String user = "xxxxx";
String host = "xxxxx";  
int port = 22;
String pass = "xxxxx";

public String conectar() throws Exception {     

    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, 22);
    session.setPassword(pass);

    // Avoid asking for key confirmation
    Properties prop = new Properties();
    prop.put("StrictHostKeyChecking", "no");
    session.setConfig(prop);

    session.connect();

    // SSH Channel
    ChannelExec channelssh = (ChannelExec) 
                    session.openChannel("exec");      
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    channelssh.setOutputStream(baos);

    // Execute command
    channelssh.setCommand("ls");
    channelssh.connect();        
    channelssh.disconnect();

    return baos.toString();
}

日志猫:

 FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.example.sarai55.ConexionSSH
at com.example.sarai55.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

从https://github.com/shikhar/sshj/downloads下载最新版本的 SSHJ 库 解压缩 zip 文件并将“sshj-0.8.1.jar”复制到您的 android 项目的 libs 目录中。在构建路径中也包括 SSHJ 库的依赖项。SLF4J - 日志库。(slf4j-api-1.6.99.jar, slf4j-simple-1.6.99.jar) Bounty Castle – 安全库。(bcpkix-jdk15on-147.jar, bcprov-jdk15on-147.jar) JZlib – 压缩库。() 将上述jar 文件也包含到Android 项目的lib 文件夹中。

于 2012-11-20T10:16:06.480 回答