【问题标题】:Spring Boot Application Could not initialize class org.apache.logging.log4j.util.PropertiesUtilSpring Boot 应用程序无法初始化类 org.apache.logging.log4j.util.PropertiesUtil
【发布时间】:2018-11-13 03:47:17
【问题描述】:

我正在尝试按照"Building a RESTful Web Service" 使用spring boot、gradle 和tomcat 来实现hello world web 应用程序,但一直无法运行让它运行至今。

代码与网站上提供的代码几乎相同,我浪费了数小时调试它,以为提供的代码中存在错误,但我仍然无法找出问题所在。

我正在使用面向 Web 开发人员的 Eclipse Java EE IDE,版本:Neon.3 Release (4.6.3),内部版本 ID:20170314-1500

知道可能是什么问题吗?

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Greeting.java

package App;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

GreetingController.java

package App;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

Application.java

package App;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.getProperties().put("server.port", 8486);
        SpringApplication.run(Application.class, args);
    }
}

堆栈跟踪

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.util.PropertiesUtil
    at org.apache.logging.log4j.status.StatusLogger.<clinit>(StatusLogger.java:71)
    at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:60)
    at org.apache.commons.logging.LogFactory$Log4jLog.<clinit>(LogFactory.java:199)
    at org.apache.commons.logging.LogFactory$Log4jDelegate.createLog(LogFactory.java:166)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:109)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:99)
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:198)
    at App.Application.main(Application.java:9)

【问题讨论】:

  • 你的 application.properties 中有什么东西吗?
  • 我的项目中没有application.properties 文件。
  • 可以把你的项目结构放到github上吗?

标签: java spring spring-boot gradle


【解决方案1】:

使用授权管理器时,此消息也可能是catalina.policysecurity.policy (-Djava.security.policy) 中缺少授权(或文件)的结果。

例如添加:

grant codeBase "file:${catalina.base}/webapps/${APPLICATION_NAME}/-" {
    permission java.security.AllPermission;
};

【讨论】:

    【解决方案2】:

    显然使用System.getProperties().put("server.port", 8486); 设置端口号NoClassDefFoundError 异常。

    但是,在资源文件夹中创建 @Nitishkumar Singh 提到的 application.properties 文件以指定要使用的端口号解决了该问题。

    【讨论】:

      【解决方案3】:

      添加这个依赖:

      compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'
      

      然后再试一次。

      【讨论】:

      • 已经存在传递依赖org.springframework.boot:spring-boot-starter-web -> org.springframework.boot:spring-boot-starter -> org.springframework.boot:spring-boot-starter-logging
      • 这并没有改变任何东西。
      【解决方案4】:

      无法初始化类 org.apache.logging.log4j.util.PropertiesUtil。 我遇到了和你一样的问题。始终使用 Jenkins 自动部署到 Tomcat。可能有些 jar 不排除依赖 'org.apache.logging.log4j'。但是 springboot 2.0.1.RELEASE 默认使用 logback。在 org.springframework.boot.web.servlet.support.SpringBootServletInitializer 上:

      public void onStartup(ServletContext servletContext) throws ServletException {
              this.logger = LogFactory.getLog(this.getClass());
              WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
              if (rootAppContext != null) {
                  servletContext.addListener(new ContextLoaderListener(rootAppContext) {
                      public void contextInitialized(ServletContextEvent event) {
                      }
                  });
              } else {
                  this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
              }
      
          }
      
           public static Log getLog(String name) {
              switch(logApi) {
              case LOG4J:
                  return LogFactory.Log4jDelegate.createLog(name);
              case SLF4J_LAL:
                  return LogFactory.Slf4jDelegate.createLocationAwareLog(name);
              case SLF4J:
                  return LogFactory.Slf4jDelegate.createLog(name);
              default:
                  return LogFactory.JavaUtilDelegate.createLog(name);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2022-01-18
        • 2019-01-22
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 2015-03-03
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        相关资源
        最近更新 更多