简体   繁体   English

使用gradle构建框架使用log4j包构建应用程序时出错

[英]Build error of app using log4j package using gradle build framework

I created a simple java project using gradle, starting with "gradle init --type java-application". 我使用gradle创建了一个简单的Java项目,从“ gradle init --type java-application”开始。

Content of main java file - "App.java": 主要Java文件的内容-“ App.java”:

import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;


public class App {

    public static void main(String[] args) {
        System.out.println("Howdy");
    }
}

Content of file "build.gradle": 文件“ build.gradle”的内容:

apply plugin: 'java'
apply plugin: 'application'

repositories {
    jcenter()
}

dependencies {
    compile 'com.google.guava:guava:21.0'
    testCompile 'junit:junit:4.12'

    // Following added by me for log4j
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'

}

mainClassName = 'App'

The log4j jar file seems to have been downloaded successfully by gradle: log4j jar文件似乎已通过gradle成功下载:

/home/ahmed/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.11.0/bede79a3f150711634a3047985517431bf6499f2/log4j-api-2.11.0.jar
/home/ahmed/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-core/2.11.0/e6b751e02120c08702d98750f6a80bc25343b7f5/log4j-core-2.11.0.jar

There are a bunch of log4j jar/pom files in ~/.gradle directory. 〜/ .gradle目录中有一堆log4j jar / pom文件。 When I try to build, I am getting the following build error: 当我尝试构建时,出现以下构建错误:

$ gradle build
:compileJava
/home/ahmed/temp/javatut/gradle-demo/src/main/java/App.java:5: error: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
                       ^
1 error
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 0.808 secs

I am new to gradle/java. 我是gradle / java的新手。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you, Ahmed. 谢谢,艾哈迈德。

In build.gradle file, you are specify dependencies of log4j2 jars, but, in code, you are using log4j (version 1) class build.gradle文件中,您指定了log4j2 jar的依赖关系,但是在代码中,您正在使用log4j (版本1)类

import org.apache.log4j.Logger;

This statement specifies the Logger class which is present in log4j version 1. 该语句指定log4j版本1中存在的Logger类。

In log4j2 , it is changed to org.apache.logging.log4j.Logger - log4j2 ,将其更改为org.apache.logging.log4j.Logger

import org.apache.logging.log4j.Logger;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM