使用 Jsch 发出 Java 命令时遇到问题
我在通过 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 文件的直接路径。
I'm having an issue running a java commend via jsch. This java command works fine if I execute it in a SSH session via putty, but when executed in my code returns an exit status of 127.
The command is this:
sshCommManager.sendCommand("cmd /c java -Xms256M -Xmx1024M -jar FileCatalystTester.jar -basic /Y");
With the sendCommand function being a derivative of the exec example. Here is the entire SSHCommManager I wrote:
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;
}
}
...any thoughts or help would be appreciated as I cannot figure out why that java command won't work. I've tried everything, including putting in the direct paths for both java and the .jar file I'm trying to execute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下程序可以访问远程 SSH 服务器
Following program work to access remote SSH server