@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
String reqURI = req.getRequestURI();
reqURI = reqURI.replace(req.getContextPath(), "");
try {
ServiceFactory factory = ServiceFactory.getInstance();
Service service = factory.getService(reqURI);
service.doPost(req, resp);
} catch (Exception e ) {
ROOT_LOGGER.error(e.getMessage(), e);
throw new ServletException(e);
}
}
当我尝试获取ServiceFactory实例时,出现了NoClassDefFoundError。
只有在部署应用程序之后才会发生。如果我通过IntelliJ启动它,则不会发生错误。
有什么问题 ?
public class ServiceFactory {
private static final Map<String, Service> SERVICE_MAP = new HashMap<>();
private static final ServiceFactory SERVICE_FACTORY = new ServiceFactory();
private ServiceFactory() {
init();
}
private static void init() {
SERVICE_MAP.put(LOGIN_PAGE_URI, new LoginService());
SERVICE_MAP.put(LOGOUT_PAGE_URI, new LogoutService());
SERVICE_MAP.put(SWITCH_LANGUAGE_URI, new SwitchLanguageService());
SERVICE_MAP.put(USERS_PAGE_URI, new AllUsersService());
SERVICE_MAP.put(REGISTRATION_PAGE_URI, new RegistrationService());
SERVICE_MAP.put(DELETE_USER_PAGE_URI, new DeleteUserService());
SERVICE_MAP.put(NEW_DOCUMENT_PAGE_URI, new NewDocumentService());
SERVICE_MAP.put(GET_FORM_AJAX_PAGE_URI, new GetFormAJAX());
}
public static ServiceFactory getInstance() {
return SERVICE_FACTORY;
}
public Service getService(String request) {
return SERVICE_MAP.get(request);
}
最佳答案
根据您提供的信息,您可能不会将导致NoClassDefFoundError的库包含到类路径中
java -cp <add-jar-paths-with-file-separator> <class-to-run> <arguments>
例如
java -cp lib/the-jar-you-are-missing.jar;myapp.jar com.mypackage.MyClassWithMain arg1
对于linux替换;字符:
如果您的库有固定的路径,也可以将路径添加到MANIFEST.MF
Manifest-Version: 1.0
...
Main-Class: com.mypackage.MyClassWithMain
Class-Path: lib/the-jar-you-are-missing.jar
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Intellij不会自动为您执行此操作。可能有一种方法可以做到,但是不幸的是我不知道答案。
关于java - NoClassDefFoundError静态字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58798073/