Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JSch X11转发时不显示_Java_Eclipse_Ssh_X11_Jsch - Fatal编程技术网

Java 使用JSch X11转发时不显示

Java 使用JSch X11转发时不显示,java,eclipse,ssh,x11,jsch,Java,Eclipse,Ssh,X11,Jsch,我试图使用JSch自动化一个过程,在这个过程中,我们的用户必须通过ssh连接到特定的工作站来运行GUI应用程序(由于许可问题)。所有工作站都运行CentOS Linux。该过程将涉及用户单击其机器上GUI应用程序中的按钮,输入密码,应用程序将处理ssh并在远程机器上打开GUI应用程序。我尝试使用代码实现自己的X11转发;当我尝试在远程机器上运行任何GUI应用程序时,它将进行身份验证和连接,但会挂起。我可以运行诸如uptime之类的程序并接收文本响应,该响应告诉我ssh连接正在工作,但X11转发没

我试图使用JSch自动化一个过程,在这个过程中,我们的用户必须通过ssh连接到特定的工作站来运行GUI应用程序(由于许可问题)。所有工作站都运行CentOS Linux。该过程将涉及用户单击其机器上GUI应用程序中的按钮,输入密码,应用程序将处理ssh并在远程机器上打开GUI应用程序。我尝试使用代码实现自己的X11转发;当我尝试在远程机器上运行任何GUI应用程序时,它将进行身份验证和连接,但会挂起。我可以运行诸如
uptime
之类的程序并接收文本响应,该响应告诉我ssh连接正在工作,但X11转发没有。当这不起作用时,我复制并粘贴了如下示例代码:

/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
* This program will demonstrate X11 forwarding.
*   $ CLASSPATH=.:../build javac X11Forwarding.java 
*   $ CLASSPATH=.:../build java X11Forwarding
* You will be asked username, hostname, displayname and passwd. 
* If your X server does not run at 127.0.0.1, please enter correct
* displayname. If everything works fine, you will get the shell prompt.
* Try X applications; for example, xlogo.
*
*/
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;

public class X11Forwarding{
public static void main(String[] arg){

String xhost="127.0.0.1";
int xport=0;

try{
  JSch jsch=new JSch();  

  String host=null;
  if(arg.length>0){
    host=arg[0];
  }
  else{
    host=JOptionPane.showInputDialog("Enter username@hostname",
                                     System.getProperty("user.name")+
                                     "@localhost"); 
  }
  String user=host.substring(0, host.indexOf('@'));
  host=host.substring(host.indexOf('@')+1);

  Session session=jsch.getSession(user, host, 22);

  String display=JOptionPane.showInputDialog("Please enter display name", 
                     xhost+":"+xport);
  xhost=display.substring(0, display.indexOf(':'));
  xport=Integer.parseInt(display.substring(display.indexOf(':')+1));

  session.setX11Host(xhost);
  session.setX11Port(xport+6000);

  // username and password will be given via UserInfo interface.
  UserInfo ui=new MyUserInfo();
  session.setUserInfo(ui);
  session.connect();

  Channel channel=session.openChannel("shell");

  channel.setXForwarding(true);

  channel.setInputStream(System.in);
  channel.setOutputStream(System.out);

  channel.connect();
}
catch(Exception e){
  System.out.println(e);
}
}

public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){
  Object[] options={ "yes", "no" };
  int foo=JOptionPane.showOptionDialog(null, 
         str,
         "Warning", 
         JOptionPane.DEFAULT_OPTION, 
         JOptionPane.WARNING_MESSAGE,
         null, options, options[0]);
   return foo==0;
}

String passwd;
JTextField passwordField=(JTextField)new JPasswordField(20);

public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return true; }
public boolean promptPassword(String message){
  Object[] ob={passwordField}; 
  int result=
  JOptionPane.showConfirmDialog(null, ob, message,
                JOptionPane.OK_CANCEL_OPTION);
  if(result==JOptionPane.OK_OPTION){
passwd=passwordField.getText();
return true;
  }
  else{ return false; }
}
public void showMessage(String message){
  JOptionPane.showMessageDialog(null, message);
}
final GridBagConstraints gbc = 
  new GridBagConstraints(0,0,1,1,1,1,
                         GridBagConstraints.NORTHWEST,
                         GridBagConstraints.NONE,
                         new Insets(0,0,0,0),0,0);
private Container panel;
public String[] promptKeyboardInteractive(String destination,
                                          String name,
                                          String instruction,
                                          String[] prompt,
                                          boolean[] echo){
  panel = new JPanel();
  panel.setLayout(new GridBagLayout());

  gbc.weightx = 1.0;
  gbc.gridwidth = GridBagConstraints.REMAINDER;
  gbc.gridx = 0;
  panel.add(new JLabel(instruction), gbc);
  gbc.gridy++;

  gbc.gridwidth = GridBagConstraints.RELATIVE;

  JTextField[] texts=new JTextField[prompt.length];
  for(int i=0; i<prompt.length; i++){
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = 0;
    gbc.weightx = 1;
    panel.add(new JLabel(prompt[i]),gbc);

    gbc.gridx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weighty = 1;
    if(echo[i]){
      texts[i]=new JTextField(20);
    }
    else{
      texts[i]=new JPasswordField(20);
    }
    panel.add(texts[i], gbc);
    gbc.gridy++;
  }

  if(JOptionPane.showConfirmDialog(null, panel, 
                                   destination+": "+name,
                                   JOptionPane.OK_CANCEL_OPTION,
                                   JOptionPane.QUESTION_MESSAGE)
     ==JOptionPane.OK_OPTION){
    String[] response=new String[prompt.length];
    for(int i=0; i<prompt.length; i++){
      response[i]=texts[i].getText();
    }
return response;
  }
  else{
    return null;  // cancel
  }
}
}
}
/*-*-模式:java;c-基本偏移量:2;缩进制表符模式:nil-*-*/
/**
*此程序将演示X11转发。
*$CLASSPATH=.:../build javac X11Forwarding.java
*$CLASSPATH=.:../build java x11前进
*将询问您的用户名、主机名、显示名和密码。
*如果您的X服务器未在127.0.0.1运行,请输入正确的
*显示名称。如果一切正常,您将得到shell提示。
*尝试X应用程序;例如,xlogo。
*
*/
导入com.jcraft.jsch.*;
导入java.awt.*;
导入javax.swing.*;
公共类X11正向{
公共静态void main(字符串[]arg){
字符串xhost=“127.0.0.1”;
int-xport=0;
试一试{
JSch JSch=新的JSch();
字符串host=null;
如果(参数长度>0){
主机=arg[0];
}
否则{
host=JOptionPane.showInputDialog(“输入username@hostname",
System.getProperty(“user.name”)+
“@localhost”);
}
字符串user=host.substring(0,host.indexOf('@');
host=host.substring(host.indexOf('@')+1);
Session Session=jsch.getSession(用户,主机,22);
String display=JOptionPane.showInputDialog(“请输入显示名称”,
xhost+“:”+xport);
xhost=display.substring(0,display.indexOf(':');
xport=Integer.parseInt(display.substring(display.indexOf(“:”)+1));
session.setX11Host(xhost);
session.setX11Port(xport+6000);
//用户名和密码将通过UserInfo界面提供。
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
Channel=session.openChannel(“shell”);
通道设置向前(真);
channel.setInputStream(System.in);
channel.setOutputStream(系统输出);
channel.connect();
}
捕获(例外e){
系统输出打印ln(e);
}
}
公共静态类MyUserInfo实现UserInfo,UIKeyboardInteractive{
公共字符串getPassword(){return passwd;}
公共布尔promptYesNo(字符串str){
对象[]选项={“是”、“否”};
int foo=JOptionPane.showOptionDialog(null,
str,
“警告”,
JOptionPane.DEFAULT_选项,
JOptionPane.WARNING_消息,
null,选项,选项[0]);
返回foo==0;
}
字符串密码;
JTextField passwordField=(JTextField)新的JPasswordField(20);
公共字符串getPassphrase(){return null;}
公共布尔提示密码短语(字符串消息){return true;}
公共布尔提示密码(字符串消息){
对象[]ob={passwordField};
整数结果=
JOptionPane.showConfirmDialog(null、ob、message、,
JOptionPane.OK\u CANCEL\u选项);
if(result==JOptionPane.OK\u选项){
passwd=passwordField.getText();
返回true;
}
else{return false;}
}
公共void showMessage(字符串消息){
showMessageDialog(空,消息);
}
最终GRIDBAG约束gbc=
新的网格约束(0,0,1,1,1,1,
格林巴奇,西北,
gridbag.NONE,
新插图(0,0,0,0),0,0);
私人货柜小组;
公共字符串[]promptKeyboardInteractive(字符串目标,
字符串名,
字符串指令,
字符串[]提示符,
布尔值[]回声){
panel=新的JPanel();
panel.setLayout(新的GridBagLayout());
gbc.weightx=1.0;
gbc.gridwidth=GridBagConstraints.rements;
gbc.gridx=0;
面板添加(新JLabel(说明),gbc);
gbc.gridy++;
gbc.gridwidth=GridBagConstraints.RELATIVE;
JTextField[]text=新的JTextField[prompt.length];
对于(int i=0;i