How to upgrade javax.naming.* and javax.xml.* impo

2019-06-14 18:28发布

问题:

I have a java project built with maven and jdk 1.8. Some classes use javax.naming.* and javax.xml.* classes in the jdk. From Java 9 onwards those javax packages have been removed from the jdk. So how do I upgrade the project to run on java 9 or later?

Here are the classes,

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NoInitialContextException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.event.EventContext;
import javax.naming.event.EventDirContext;
import javax.naming.event.NamingListener;
import javax.naming.ldap.Control;
import javax.naming.ldap.ExtendedRequest;
import javax.naming.ldap.ExtendedResponse;
import javax.naming.ldap.LdapContext;
import javax.naming.spi.InitialContextFactory;
import javax.naming.spi.InitialContextFactoryBuilder;
import javax.naming.spi.NamingManager;
import javax.xml.namespace.QName;

I have found out jndi contains javax.naming.* classes. But I am not sure how to include them. Just adding as a maven dependency does not work.

回答1:

These classes have not been removed, see the JavaDoc for javax.naming and javax.xml.namespace. Unless you are building a module and forgot to add the modules java.xml and java.naming as dependencies, the mistake lies outside of Java, most likely with the tooling.

To convince yourself, compile a simple class on the command line with javac and watch it work. Then make sure you are running recent versions of the involved tools (particularly build tool and IDE). If that's the case and the error persists, improve your question with a more detailed description of the error.