NoClassDefFoundError 静态字段
NoClassDefFoundError static flieds
@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 的库包含到您的 class-path
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 不会自动为您执行此操作。可能有办法做到这一点,但不幸的是我不知道答案。
@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 的库包含到您的 class-path
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 不会自动为您执行此操作。可能有办法做到这一点,但不幸的是我不知道答案。