簡體   English   中英

Spring Boot 應用程序無法初始化類 org.apache.logging.log4j.util.PropertiesUtil

[英]Spring Boot Application Could not initialize class org.apache.logging.log4j.util.PropertiesUtil

我正在嘗試通過遵循“構建 RESTful Web 服務”使用 spring boot、gradle 和 tomcat 來實現 hello world Web 應用程序,但到目前為止一直無法運行。

代碼與網站上提供的代碼幾乎相同,我浪費了幾個小時調試它,認為提供的代碼中存在錯誤,但我仍然無法弄清楚出了什么問題。

我正在使用 Eclipse Java EE IDE for Web Developers,版本:Neon.3 Release (4.6.3),Build id:20170314-1500

知道可能是什么問題嗎?

構建.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')
}

問候語.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;
    }
}

問候控制器.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));
    }
}

應用程序.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)

使用授權管理器時,此消息也可能是 catalina.policy 中缺少授權的結果。

例如添加:

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

顯然使用System.getProperties().put("server.port", 8486);設置端口號System.getProperties().put("server.port", 8486); NoClassDefFoundError異常。

但是建立一個application.properties通過@Nitishkumar辛格提到的文件中的資源文件夾到指定的端口號的使用解決了這個問題。

添加此依賴項:

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

然后再試一次。

無法初始化類 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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM