[Unresolved] An exception occurs when an SSH connection is executed using JSch from a Java 6 application.

Thing you want to do

It is to make an SSH connection to another machine from a self-made application compiled with Java6.

environment

access point

client

Problem summary

When you call the API of JSch and make an SSH connection to another machine, it will be in JSch. Will raise an exception.

Problem details

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.

solution

No way to avoid the exception has been found yet. I wonder if something can be done.

Recommended Posts

[Unresolved] An exception occurs when an SSH connection is executed using JSch from a Java 6 application.
File path is not recognized when executing an external command from a Java application
Ssh connect using SSHJ from a Java 6 app
When using a list in Java, java.awt.List comes out and an error occurs
When using ExpandableListView with fragment, exception occurs when inheritance is ListFragment
Collecting client information when an error occurs in a web application
Access Teradata from a Java application
Unexpected exception when using Java DateTimeFormatter
Java Exception StackTrace is determined when new
A reminder when an aapt.exe error occurs
Corresponds to a property whose type is an array when empty using JsonDeserializer
Let's create a TODO application in Java 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server
Connect to Aurora (MySQL) from a Java application
Make an ssh connection from Mac / VirtualBox (CentOS)
[Java] Get a random value from an array
I want to issue a connection when a database is created using Spring and MyBatis
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID