'How to use Java EE and Jakarta EE together?

Is it at all possible to use older Jave EE libraries (with javax imports) in a new Jakarta EE system (with jakarta imports)?

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, and mail may bite me too.



Solution 1:[1]

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'a a work in progress, but does the job quite well already.

Solution 2:[2]

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));
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arjan Tijms
Solution 2