2

I am trying to get a remote connection to server to execute some commands. I am new to this so started googling. After some search, I gave a try:

I am using sshj0.2.3.jar.

And here is how I implemented it:

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    final SSHClient ssh = new SSHClient();
    ssh.loadKnownHosts();
    ssh.connect("serverName");
    try{
        ssh.authPublickey("myUserId");
        final Session session = ssh.startSession();
        try{
            final Command cmd = session.exec("net send myMachineName Hello!!!");
            System.out.println(cmd.getOutputAsString());
            System.out.println("\n Exit Status: "+cmd.getExitStatus());
        }finally{
            session.close();
        }
        }finally{
            ssh.disconnect();
        }   
    }

}

But I get the folowing exception:

    Exception in thread "main" java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
    at net.schmizz.sshj.DefaultConfig.(DefaultConfig.java:92)
    at net.schmizz.sshj.SSHClient.(SSHClient.java:133)
    at SSHTEST.main(SSHTEST.java:24)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:419)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:643)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:345)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:609)
    .....

Do I need extra jars,How many extra jars do i need. Am I implementing correctly?. please help.

Thanks in advance.

3 Answers 3

3

From googling sshj, the first result reveals the answer:

Dependencies

Java 6+. slf4j is required. bouncycastle is highly recommended and required for using some of the crypto algorithms. jzlib is required for using zlib compression.

Sign up to request clarification or add additional context in comments.

5 Comments

I am using 1.5 java. Is there another approach. I just want to execute some commands in remote server. Thanks.
Yes - you need slf4j to resolve the specific message you're seeing.
I downloaded the zip file from slf4j.org/download.html but i am getting the same error.
I am getting this exceptions now: Exception in thread "main" java.io.IOException: Could not load known_hosts at net.schmizz.sshj.SSHClient.loadKnownHosts(SSHClient.java:528) at SSHTEST.main(SSHTEST.java:25)
The library reads the known_hosts file in the default location which is ~/.ssh/known_hosts. The easiest way to create one is to use ssh to login once to the remote server. ssh asks confirmation and creates an entry for the remote host. From now on Java can use this host safely. If you want to use a file from another location, use the other version of loadKnownHost with the location of the file as parameter. This might be useful on Windows or example.
2

Yes, you need the jars for slf4j (see the 2-page manual)

slf4j comes with a API and then a pluggable implementation, but the proposed slf4j-simple should be just fine

4 Comments

I am using 1.5 java. Is there another approach. I just want to execute some commands in remote server. Thanks.
slf4j has no special requirements, and is AFAIK compatible with Java 1.4. If you drop this in the classpath this error will go away, and you'll be able to follow the log messages on the console
I am getting this exceptions now: Exception in thread "main" java.io.IOException: Could not load known_hosts at net.schmizz.sshj.SSHClient.loadKnownHosts(SSHClient.java:528) at SSHTEST.main(SSHTEST.java:25)
The library reads the known_hosts file in the default location which is ~/.ssh/known_hosts. The easiest way to create one is to use ssh to login once to the remote server. ssh asks confirmation and creates an entry for the remote host. From now on Java can use this host safely. If you want to use a file from another location, use the other version of loadKnownHost with the location of the file as parameter. This might be useful on Windows or example.
1

slf4j is just a facade, and you need to supply a concrete implementation. If you don't really care about logging, stick in slf4j-nop-$(ver).jar that is present in the slf4j distribution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.