gpt4 book ai didi

java - 我正在使用 JSch 以及未在子类中继承的连接和 session 对象

转载 作者:行者123 更新时间:2023-12-01 14:03:55 25 4
gpt4 key购买 nike

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;

public class ImageTypeDeployer extends MainImageDeployer {

public ImageTypeDeployer(String VolID,String oS,String imageName){

String command="some command"

try {
channel=session.openChannel("exec");
channel.setInputStream(null);
((ChannelExec)channel).setCommand(command);
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

我在 line channel = session.openChannel("exec") 上收到 NullPointerException当我将代码放入父类(super class)中时,同样的事情也会发生。这是因为 session 和 channel 变量不是继承的还是其他原因?

这是我正在建立连接的父类(super class)代码。

public void makeAConnection(){

inputFile = RESTEngine.getFilePath();
Properties defaultProps = new Properties();
try {
fin = new FileInputStream(inputFile);
defaultProps.load(fin);
fin.close();
}
catch(FileNotFoundException e1){
System.out.println("The properties file supposed to contain PowerVC Authorization parameters was not found.");
e1.printStackTrace();
System.exit(-1);
}
catch(IOException e1){
System.out.println("An exception occured while trying to open the properties file");
e1.printStackTrace();
System.exit(-1);
}
// assign variables from Input file with default value as null
user = defaultProps.getProperty("UserID", null);
host = defaultProps.getProperty("PowerVC_IP_ADDRESS", null);
password = defaultProps.getProperty("UserPass" ,null );

jsch = new JSch();
try {
session = jsch.getSession(user, host, 22);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel=session.openChannel("exec");
channel.setInputStream(null);

try {
in = channel.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Connection Successful");
} catch (JSchException e) {
// TODO Auto-generated catch block
System.out.println("Unable to connect");
e.printStackTrace();
}

}

这就是我调用 ImageTypeDeployer 的方式

protected Boolean doInBackground() throws Exception {

makeAConnection();
for(String imageName : volIDMap.keySet()){

String volID = volIDMap.get(imageName);
String oS = osMap.get(imageName);
if (oS.equalsIgnoreCase("aix")){

MainImageDeployer imageDeployer = new ImageTypeDeployer(volID, oS, imageName);

}


}
return null;



}

最佳答案

看来 session 字段未在 ImageTypeDeployer 或其任何父类(super class)的构造函数中初始化,因此当您尝试调用 session 时,该字段为 null。打开 channel 。这就解释了需要在 makeAConnection 方法中初始化它。

关于java - 我正在使用 JSch 以及未在子类中继承的连接和 session 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com