Question

I've being teached myself Java over the last few weeks (quite possibly doing something wrong)!

I have developed a simple program that connects to a database lists locations. I can connect to them, using JSCH to set up a tunnel. So I can VNC onto it.

This all works fine. However, my issue is with the disconnect part of this.

I have a Public Void called WinConnect, this is fired if the button for connection had been clicked cliked and as above works as thread.

On the disconnect button I tried:

Current Part of the relivant button code

private void jButtonDisconnectActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    this.channel.disconnect();
    this.session.disconnect(); 

}

  • Session session.disconnect(); //says expected; and cannot find symbol method disconnect

  • Session.session.disconnect(); //this says cannot find varible session

  • JSch.session.disconnect //this says cannot find varible session

and so on, none of them do work. Netbeans says there are errors within the code. Clearly my lack of knowledge means im doing something wrong, probably something obvious!

All Of WinConnect:

 private void WinConnect(){
  String SshPortNumb = (String)SshPort.getSelectedItem();
  int SshPortNum = Integer.parseInt(SshPortNumb);
  String PcIpNum = (String)PcIp.getText();
  String host = (String)SiteIp.getText();
  String VncPortNumb = VncPort.getText();
  int VncPortNum = Integer.parseInt(VncPortNumb);
  String user = (String)UserName.getText();
  String PasswordVal = (String)Password.getText();
  String VncPathpre = VncPath.getText();
  String VncPathdat = "\""+VncPathpre+"\"";

try{
  JSch jsch=new JSch();

  jsch.setKnownHosts("known_hosts");


  this.session=jsch.getSession(user, host, SshPortNum);

  String passwd = PasswordVal;
  this.session.setPassword(passwd);

  UserInfo ui = new MyUserInfo(){
    public void showMessage(String message){
      JOptionPane.showMessageDialog(null, message);
    }
    public boolean promptYesNo(String message){
      Object[] options={ "yes", "no" };
      int foo=JOptionPane.showOptionDialog(null, 
                                           message,
                                           "Warning", 
                                           JOptionPane.DEFAULT_OPTION, 
                                           JOptionPane.WARNING_MESSAGE,
                                           null, options, options[0]);
      return foo==0;
    }

  };
jButtonDisconnect.setEnabled(true);
jButtonConnect.setEnabled(false);
  this.session.setUserInfo(ui);

  this.session.connect(30000);  

  this.session.setPortForwardingL(7051, PcIpNum, VncPortNum);

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

  this.channel.setInputStream(System.in);

  this.channel.setOutputStream(System.out);

  this.channel.connect(3*1000);

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

}

public class RemoteDesktop extends javax.swing.JFrame {
private Session session;
private Channel channel;

EDIT: Guess because the varible Session is in the winconnect and not in the button it can't find it?

Edit2: After trying suggestion below it just locks up when clicking disconnect

Edit3: Right the new code should work, but there appears to be an issue with session.disconnect, if i call session.delPortForwardingL(7051); it works but im not sure if the connections are still active, unsure why session.disconnect does not!

Edit4: Checked the server and the connecetions are still in place it does just remove the fowarding stoppign my VNC, not good :(

Edit5: Checked if calling the session.disconnect was actually working it does, it however causes my program to lock up for some reason

Was it helpful?

Solution

Because you did post just a piece of source code, I only could guess. Based on the error messages, the thing is that method .addActionListener() creates an own variable scope. So you haven't an access of parents context.

Try to move the variable Session session to the global scope, something like this:

public class YourJavaClass
{
  private Session session;

  public static void main()
  {
    ...
  }

  private void connect(String location)
  {
   try
   {
     JSch jsch=new JSch();

     jsch.setKnownHosts(location);

     this.session=jsch.getSession(user, host, SshPortNum);

     String passwd = PasswordVal;
     this.session.setPassword(passwd);
     this.session.connect(30000);  

     session.setPortForwardingL(7051, PcIpNum, VncPortNum);
  }

  private void yourGuiMethod()
  {
   ...
   jButtonDisconnect.addActionListener(new ActionListener()
   {
     public void jButtonDisconnectActionPerformed(java.awt.event.ActionEvent evt)
     {
        this.session.disconnect();
     }
   });  
   ...
  }
}

You can downvote my answer if you want. Though keep in mind, at this timepoint you haven't posted enough source code. So I was only able to guess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top