1

I'm curious about how Java import works. For example, I just ran this sample log4j code, and it did not compile :

import org.apache.log4j.Logger;

import java.io.*;
import java.sql.SQLException;
import java.util.*;



public class log4jExample{
  /* Get actual class name to be printed on */
  static Logger log = Logger.getLogger(
                      log4jExample.class.getName());

  public static void main(String[] args)
                throws IOException,SQLException{

     log.debug("Hello this is an debug message");
     log.info("Hello this is an info message");
  }
}

It gives this error :

3 errors found:
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java  [line: 1]
Error: package org.apache.log4j does not exist
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java  [line: 13]
Error: cannot find symbol
  symbol:   class Logger
  location: class log4jExample
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java  [line: 13]
Error: cannot find symbol
  symbol:   variable Logger
  location: class log4jExample

But then I had another class, which has an import like this :

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;

public class HelloWorldLog4J {

And this one works fine. I'm just curious , where does Java actually find the those imported libraries? thanks

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 1
    What what the specific compiler error? – Rogue Mar 13 '15 at 00:56
  • 1
    [What is a classpath?](http://stackoverflow.com/questions/2396493/what-is-a-classpath-confused-and-need-a-human-touch-to-understand) The answers mostly talk about runtime classpath, but the same thing applies to compilation time. – Sotirios Delimanolis Mar 13 '15 at 00:56
  • 1
    Imported libraries are on your classpath, and the log4j classes are in the log4j jar file. – Elliott Frisch Mar 13 '15 at 00:58
  • @Rogue - added this in . I think I'm starting to get it though, the link Sotirious gave helps – Caffeinated Mar 13 '15 at 01:07
  • 2
    Maybe I'm not understanding your question, but your import statement looks like `import org.apache.log4j.Logger;` and you say it doesn't work but `import org.apache.logging.log4j.Logger;` does work. Notice that your import is missing the 'logging' portion before 'log4j'? This is likely why yours didn't work because it wasn't complete. – blh83 Mar 13 '15 at 01:12

1 Answers1

2

An import statement is a feature which allows you to use the simple names of types (and/or their members) in your source code rather than their fully qualified names. For example, the implicit

import java.lang.*;

which is added to all java compilation units (.java source files) allows you to write

String variable = "";

instead of

java.lang.String variable = "";

When you compile your source code, the compiler needs to be able to identify the types you use so that it can tell if you're using them correctly. The typical javac compiler has the following -cp option

-cp path or -classpath path

Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

This option allows you to specify the location of any compiled .class files (extracted or within a library archive, ex: a .jar).

When you compiled your code, presumably through an IDE or build tool, what actually got executed was something like

javac -cp "/some/folder/with/classes:log4j2.jar" com/example/log4jExample.java

In other words, it was referring to the log4j2 library which contains all the logging classes. You got a compilation error because your classpath did not contain a class called org.apache.log4j.Logger and the compiler could therefore not validate what you were trying to do. In your second attempt, the classpath did contain a class called org.apache.logging.log4j.LogManager (and the others).


org.apache.log4j.Logger is part of log4j 1. It not part of log4j 2.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724