Java linux NoClassDefFoundError但类路径是正确的

时间:2016-10-06 06:20:49

标签: java linux eclipse maven

以下程序在具有相同执行命令的Windows上运行正常,并且在windows 10中使用eclipse进行编译:

java -cp "./*" package.Connection

$ ls
bcpkix-jdk15on-1.51.jar  bcprov-jdk15on-1.51.jar jzlib-1.1.3.jar  slf4j-api-1.7.7.jar  MyProgram-1.1.jar  sshj-0.11.0.jar

但它给出NoClassDefFoundError:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: net/schmizz/sshj/transport/verification/HostKeyVerifier
     at java.lang.Class.getDeclaredMethods0(Native Method)
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
     at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
     at java.lang.Class.getMethod0(Class.java:3018)
     at java.lang.Class.getMethod(Class.java:1784)
     at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: net.schmizz.sshj.transport.verification.HostKeyVerifier
     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
            ... 7 more

我在我的班级的一个文件夹中使用maven和所有依赖项,并在此文件夹中运行命令。这很奇怪,因为该类位于sshj库中

代码:

package package;

import java.io.IOException;
import java.security.PublicKey;
import java.util.concurrent.TimeUnit;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.verification.HostKeyVerifier;

public class Connection implements AutoCloseable{

    private final Session session;
    private final SSHClient ssh;
    private final Command cmd;
    public final String output;

    public static void main(String[] args) throws Exception {
        String hostname = "hostname";
        String username = "username";
        String pathToPrivateKey = "C:\\Program Files (x86)\\PuTTY\\key-private.ppk";
        if (System.getProperty("os.name").equalsIgnoreCase("Linux")) {
            pathToPrivateKey = "/home/user/.ssh/key-private.ppk";
        }
        String command = "ls";
        try (Connection conn = new Connection(hostname, username, pathToPrivateKey, ". ./.profile ; " + command)) {
            System.out.println(conn.output);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Connection(String hostname, String username, String pathToPrivateKey, String command) throws IOException {
        ssh = new SSHClient();
        ssh.addHostKeyVerifier(new HostKeyVerifier(){

            @Override
            public boolean verify(String hostname, int port, PublicKey key) {
                // TODO Auto-generated method stub
                return true;
            }

        });  
        ssh.connect(hostname);
        try {
            ssh.authPublickey(username, pathToPrivateKey);
            session = ssh.startSession();
            try {
                cmd = session.exec(command);
                cmd.getInputStream();
                cmd.getErrorStream();
//              System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());

                this.output = IOUtils.readFully(cmd.getErrorStream()).toString();
                cmd.join(5, TimeUnit.SECONDS);
                System.out.println("\n** exit status: " + cmd.getExitStatus());
            } finally {
                session.close();
            }
        } finally {
            ssh.disconnect();
        }
    }

    @Override
    public void close() throws Exception {
        // TODO Auto-generated method stub
        session.close();
        ssh.close();
    }
}

与其他ssh库相同,所以我认为它与linux有关。任何人都可以帮忙吗?

0 个答案:

没有答案