简体   繁体   中英

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".

Content of main java file - "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":

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:

/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. 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. 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

import org.apache.log4j.Logger;

This statement specifies the Logger class which is present in log4j version 1.

In log4j2 , it is changed to org.apache.logging.log4j.Logger -

import org.apache.logging.log4j.Logger;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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