首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring Boot应用程序无法初始化类org.apache.logging.log4j.util.PropertiesUtil

Spring Boot应用程序无法初始化类org.apache.logging.log4j.util.PropertiesUtil
EN

Stack Overflow用户
提问于 2018-06-04 02:55:18
回答 4查看 7.9K关注 1票数 1

我正在尝试使用spring boot、gradle和tomcat来实现hello world web应用程序,方法是遵循"Building a RESTful Web Service",但到目前为止还无法运行。

代码与网站上提供的代码几乎相同,我花了几个小时调试它,以为提供的代码中有一个bug,但我仍然找不出哪里出了问题。

我正在为Web开发人员使用Eclipse Java EE IDE,版本: Neon.3 Release (4.6.3),Build : 20170314-1500

你知道问题出在哪里吗?

build.gradle

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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);
    }
}

堆栈跟踪

代码语言:javascript
运行
复制
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)
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-06-05 00:57:38

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

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

票数 1
EN

Stack Overflow用户

发布于 2020-05-19 20:42:51

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

例如,添加:

代码语言:javascript
运行
复制
grant codeBase "file:${catalina.base}/webapps/${APPLICATION_NAME}/-" {
    permission java.security.AllPermission;
};
票数 3
EN

Stack Overflow用户

发布于 2018-06-04 03:14:35

添加此依赖项:

代码语言:javascript
运行
复制
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'

再试一次。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50669888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档