It is to make an SSH connection to another machine from a self-made application compiled with Java6.
When you call the API of JSch and make an SSH connection to another machine, it will be in JSch. Will raise an exception.
Java 6 now? You might think that, but there were circumstances where I had to use it.
When it comes to SSH connection in Java, it seems that the JSch library of JCraft is often used. .. It is registered in the Mevan Repository and can also be downloaded from the JCraft site. I will.
That's why I immediately tried using it. The code simply looks like this:
MainClass.java
package sample.sshconnect;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class MainClass {
private static final String USER = "user_sample";
private static final String HOST = "111.222.333.444";
private static final String PASSWORD = "password_sample";
private static final int PORT = 22;
public static void main(String[] args) {
//Connecting and disconnecting to the server
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(USER, HOST, PORT);
} catch (JSchException e) {
e.printStackTrace();
return;
}
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSWORD);
try {
session.connect();
} catch (JSchException e) {
e.printStackTrace();
return;
}
session.disconnect();
}
}
In pom.xml
, specify 1.6
for maven-compiler-plugin
.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SampleSshConnect</groupId>
<artifactId>SampleSshConnect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</project>
When I run it, the following information is output to the console.
com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
at com.jcraft.jsch.Session.connect(Session.java:565)
at com.jcraft.jsch.Session.connect(Session.java:183)
at sample.sshconnect.MainClass.main(MainClass.java:27)
Apparently, there is an exception in session.connect ();
.
So, modify pom.xml
to change the version specified in maven-compiler-plugin
to 1.7
.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SampleSshConnect</groupId>
<artifactId>SampleSshConnect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</project>
When I run it, nothing is displayed on the console.
This means that when you compile with Java7, the SSH connection will be executed normally, If you compile with Java6, you will get an exception when connecting to SSH.
No way to avoid the exception has been found yet. I wonder if something can be done.
Recommended Posts