简体   繁体   中英

can I use two ssh java lib in my java code?

I am using j2ssh but due to some reasons (which we cant detect yet) we decided to switch to new ssh lib.

in j2ssh for 2 functions which are not giving expected result, we have to switch to new ssh (i have found vngx-jsch suitable)

but only for 2 functions, i need to do full rework of all ssh related function which i dont want to.
so I have following queries : -

  1. can I use both lib ?
  2. does it make sense ?
  3. what if I keep j2ssh as it is and only for those 2 function I will load vngx-ssh?
  4. is it possible?
  5. how to do it ? any example ?

Yes, It's possible. you can use the fully qualified names to resolve the conflicts. for example:

let say there are 2 packages, com.package1.A and com.package2.B which exports the same function, func() . In your Java program, you can use any one of these by calling either

package com.package1;

public class A {
    public void func() {
       System.out.println("Inside A package's func ");
    }
}

And the second is:

package com.package2;

public class B {
    public void func() {
       System.out.println("Inside B package's func ");
    }
}

Then in a separate class you can use it like:

public class App {
    public static void main(String[] args) {
        com.package1.A.func();
        com.package2.B.func();
}
}

1) can I use both lib ?

Probably yes. But you would need to try it to be sure.

The two things that would be contra-indicative would be:

  • the two libraries defining classes with the same fully qualified class names 1 , or

  • needing to accept ssh connections in your application using the two libraries using the same port.

2) does it make sense ?

Possibly. But IMO, it makes more sense to either find and fix the root problem 2 , or recode the application so that it does need the two missing functions.

3) what if I keep j2ssh as it is and only for those 2 function I will load vngx-ssh? is it possible?

It depends what you mean. If you make separate connections using the two libraries then that could work.

how to do it ? any example ?

It depends on the details of your problem. (But you are unlikely to find examples, because what you are doing sounds like an inferior approach ...)


1 - If this is a problem, it may be possible to work around the name clash using classloader magic, but you should only do that as a last resort.

2 - This is one of the advantages of open source. If there is a problem in the application / library you are using, you can find it and fix it ... and then contribute your fix back to the community.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM