0

Because of, reasons, I cannot just update the old version of the aws sdk I'm working with, but I also need some new things that are in a more recent version.

The problem is that if put both version of the sdk the project I get a "java.lang.NoSuchMethodError" because I think it's trying to use the old version of the sdk. If I delete the old one and just use the updated one it works fine. Is there a way to keep both version of the sdk and tell my java class which one to exclusively import?

  • No, not really. You said "If I delete the old one and just use the updated one it works fine", so why can't you update the version then? Only one version will be used, so it's either the old one or the new one, you can't really have them both (well, you can but that's not something you want to do). – Kayaman Oct 01 '19 at 10:10
  • I know it's not something I want to do. But before the big bosses approve my request to update the old skd weeks probably will have passed. I'm just trying to have my project work in the meantime, and when it gets approved I can do it properly. – Jane Barret Oct 01 '19 at 10:15
  • Well there's no quick hack to get your project to work, so you're going to have to figure out something else. Are you familiar with Java's classloading mechanism? If not, your choices are quite limited (i.e. wait for your bosses approval or just continue to use the older version for now). – Kayaman Oct 01 '19 at 10:24
  • 1
    Yes,as everyone might guess, I'm not the most senior java programmer. I had looked into classloading but I would have to make such a mess that I'm probably better off sitting tight and waiting. Thanks for the answer though. – Jane Barret Oct 01 '19 at 10:40

1 Answers1

0

There are a couple of ways, but they're pretty nasty.

The arguably "more correct" approach is to could use a custom classloader - see this answer for details. However, that's not exactly simple, and can lead to weird outcomes.

A simpler, but somewhat nastier approach is to get the source code of both SDKs (if available), and rename the packages. For example if we have sdk_v1 and sdk_v2, we can rename the packages to com.example.sdk.v1 and com.example.sdk.v2, Once there's no package name collision, there's no problem using two different SDKs, even in the same class - just use fully qualified imports (see answer):

com.example.sdk.v1.SomeClass.someFunc() will not collide with com.example.sdk.v2.SomeClass.someFunc()

Malt
  • 28,965
  • 9
  • 65
  • 105