我遇到了通过jsch运行java命令的问题。如果我通过putty在SSH会话中执行它,这个java命令工作正常,但是在我的代码中执行时返回退出状态127。
命令是这样的:
sshCommManager.sendCommand("cmd /c java -Xms256M -Xmx1024M -jar FileCatalystTester.jar -basic /Y");
sendCommand函数是exec示例的衍生物。这是我写的整个SSHCommManager:
public class SSHCommManager extends Observable{
private JSch jsch; //ssh library
private static String user = "what";
private static String password = "youwhat";
//private static String host = "192.168.1.1";
private static SSHCommManager sshCommManager;
private Session session;
private Channel channel;
public boolean commsConnected = false;
private int aPort = 22;
private SSHCommManager() {
this.addObserver(MainUI.getInstance());
}
public static SSHCommManager getInstance() {
if (sshCommManager == null) {
sshCommManager = new SSHCommManager();
}
return sshCommManager;
}
public void init(String aHost) {
try {
jsch = new JSch();
//System.out.println("Getting ssh session...");
session = jsch.getSession(user, aHost, aPort);
session.setX11Host(aHost);
session.setX11Port(aPort + 6000);
//System.out.println("Getting user info...");
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
System.out.println("Connecting to ssh...");
session.connect(30000);
if (session.isConnected()) {
commsConnected = true;
} else {
commsConnected = false;
}
System.out.println(commsConnected);
} catch (Exception e) {
System.out.println(e);
}
}
public void sendCommandTest(String aCommand) {
try {
Channel channel1=session.openChannel("shell");//only shell
System.out.println("Sending Test command: "+ aCommand);
channel1.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel1.getOutputStream()); // printStream for convenience
channel1.connect();
shellStream.println(aCommand);
} catch (Exception e) {
System.out.println(e);
}
if (channel.isClosed()) {
System.out.println("exit-status: "
+ channel.getExitStatus());
}
}
public String sendCommand(String aCommand){
InputStream in = null;
OutputStream out = null;
StringBuilder commandOut = new StringBuilder();
try {
channel = session.openChannel("exec");
System.out.println("Sending command: " + aCommand);
((ChannelExec) channel).setCommand(aCommand);
//channel.setInputStream(System.in);
channel.setInputStream(null);
//channel.setOutputStream(System.out);
((ChannelExec) channel).setErrStream(System.err);
in = channel.getInputStream();
//out = channel.getOutputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)break;
//System.out.print(new String(tmp, 0, i));
//System.out.println(channel.getInputStream().toString());
commandOut.append(new String(tmp, 0, i));
//setChanged();
//notifyObservers(System.err.toString() + "\n");
}
if (channel.isClosed()) {
System.out.println("exit-status: "
+ channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
throw new JSchException("Cannot execute remote command: " + aCommand + " : " + ee.getMessage());
}
}
//channel.disconnect();
//session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
return commandOut.toString();
}
public void cleanupSSH() {
channel.disconnect();
session.disconnect();
}
public boolean isCommsConnected() {
return commsConnected;
}
}
...任何想法或帮助都会受到赞赏,因为我无法弄清楚为什么java命令不起作用。我已经尝试了所有方法,包括为java和我正在尝试执行的.jar文件添加直接路径。
答案 0 :(得分:1)
以下程序工作以访问远程SSH服务器
private String userName = "xxxx";
private String password ="xxxx";
private String hostName = "xxx.xxx.xxx.com";
private int port = 22;
private String sdstestCommand = "java -Xms256M -Xmx1024M -jar MyProgram";
public void testconnect () throws JSchException, IOException {
JSch jsch = new JSch();
Session session = jsch.getSession(userName, hostName, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(sdstestCommand);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
System.out.println("Unix system connected...");
byte[] tmp = new byte[1024];
while (true){
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
String line = new String(tmp, 0, i);
System.out.println("Unix system console output: " +line);
}
if (channel.isClosed()){
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee){
//ignore
}
}
channel.disconnect();
session.disconnect();
}
}