9

Is it at all possible to use older Java EE libraries (with javax package) in new Jakarta EE applications (with jakarta package)?

All the APIs would be backwards compatible if it weren't for the trademark issue. Is there any runtime library or build tool available that can get them to work together? Is the only solution to fork old libraries and updated them manually?

I'm in particular looking at the Servlet API, but validation, persistence, webservices and mail may bite me too.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • 1
    you could try using a custom class loader that redirects `javax` imports to `jakarta` imports if the `jakarta` imports exist. – dan1st Jan 26 '22 at 11:10

3 Answers3

6

You can use the Eclipse Transformer project to transform your older Java EE compatible jar with javax.* imports to a jar using jakarta.* imports.

The transformer takes care of some additional things like renaming constants in configuration files and such. It's a work in progress, but does the job quite well already.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
0

Like you mention, the APIs are compatible except for the trademark changes, so one solution is to build wrappers around them that delegate from the new library to the old. Might not always be straightforward, but depending on your use case, it could work.

For example:

public class LegacyServlet implements jakarta.servlet.Servlet {
 
  private final javax.servlet.Servlet delegate;
 
  public LegacyServlet(javax.servlet.Servlet delegate) {
    this.delegate = delegate;
  }
 
  @Override
  public void service(jakarta.servlet.ServletRequest req, jakarta.servlet.ServletResponse resp) {
    delegate.service(new LegacyServletRequest(req), new LegacyServletResponse(resp));
  }
}
Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • That would be quite a lot of work, though e.g. Lombok can help generating all the methods. You also have to wrap everything in the library that takes or returns a `javax` object. – OrangeDog Jan 29 '22 at 11:54
  • It's not possible to do this for annotations though, is it? – OrangeDog Jan 29 '22 at 14:01
  • Indeed, it won't work for annotations. If you don't mind me asking, why do you want to do this? Whichever solution you eventually settle for will most likely take more time to implement than just updating the libraries and doing a replace all on the imports to change them from javax to jakarta. – Bogdan Jan 29 '22 at 16:46
0

So many brief answers that make this sound like a quick, easy, trivial task. My task requires me to load a dependency (jar) into my SpringBoot3/Spring6/JDK17 environment, and operate controllers (classes that must be loaded as binary from jars I can't modify) which expect javax.http.ServletRequests. My controllers receive jakarta.http.ServletRequests and what I need to do is effectively change that jakarta.* request into a javax.* type to send it to the delegate controller.

That's turning out to be a much more complicated problem than it sounds like and certainly a bigger issue than the one- or two-line StackOverflow responses would suggest.

  • 1
    Can you not just run the jars you can't modify through Eclipse Transformer, as the top answer suggests? – OrangeDog Mar 30 '23 at 08:10