Link to home
Start Free TrialLog in
Avatar of rama_krishna
rama_krishna

asked on

Using log4j

I am new to Java programming.
I have J2SDK1.5.0 in my machine
I was able find C:\Program Files\Java\j2re1.5.0  and C:\Program Files\Java\j2sdk1.5.0 and their respective folders with file.
I was trying to compile some java file which has log4j calling
The file Log4jDemo.java contains the following:

import org.apache.log4j.Logger;

public class Log4jDemo {

      static Logger log = Logger.getLogger("Log4jDemo");

      public static void main(String args[]) {

            log.debug("This is my debug message.");
            log.info("This is my info message.");
            log.warn("This is my warn message.");
            log.error("This is my error message.");
            log.fatal("This is my fatal message.");
      }
}

Before compiling i have extrated the log4j.jar from apache site file and kept at "C:\Program Files\Java\j2sdk1.5.0\lib" folder
I am able to compile file by
C:\Program Files\Java\test>javac -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar" Log4jDemo.java

But when i do

C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at Log4jDemo.<clinit>(Log4jDemo.java:5)

Why this error is coming?
Avatar of Gibu George
Gibu George
Flag of India image

run it likk java -classpath C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar Log4jDemo
Avatar of keyurkarnik
keyurkarnik

add the classpath line again
or else add the log4j jar file to the CLASSPATH env variable
java -classpath C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar Log4jDemo

opena a command prompt and type :

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar

Then run yyour program as java Log4jDemo
Your classpath tells the JVM where to find dependent classes. To compile (for javac) you specifed the classpath. Similarly you need to specify the classpath for running your class as well (java). (Ensure that you include the current directory (.) (a dot) as part of the classpath.

So you have 3 options:

1. set CLASSPATH=.;C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar;%CLASSPATH%
    and then: java Log4jDemo
2. java -classpath=.;C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar;%CLASSPATH Log4jDemo
3. Copy the log4j.jar under C:\Program Files\Java\j2sdk1.5.0\ext\ (you may have to create an ext folder) and run
java -classpath=. Log4jDemo.

If you intend to follow good practice, then include this class (the Log4Demo) as under a package.

Hope that helps.
Avatar of CEHJ
Put log4j.jar in C:\Program Files\Java\j2sdk1.5.0\jre\lib\ext
> Put log4j.jar in C:\Program Files\Java\j2sdk1.5.0\jre\lib\ext

Please stop repeating comments from other experts
>>Please stop repeating comments from other experts

And which one would that be?
you are also supposed to actually read previous comments before posting, please read the EE guidelines and try to follow them.
I have read them
Avatar of rama_krishna

ASKER

I tried the following options:
1).
C:\Program Files\Java\test>java -classpath C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundError: Files\Java\j2sdk1/5/0\lib\log4j/jar
Problem still persists
2).
Set the Classpath first
C:\Program Files\Java\test>set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar

C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundError: Log4jDemo
Problem still persists

3).
C:\Program Files\Java\test>java -classpath=.;C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar;%CLASSPATH Log4jDemo
Unrecognized option: -classpath=.;C:\Program
Could not create the Java virtual machine.
Problem still persists

4).
Copy the log4j.jar under C:\Program Files\Java\j2sdk1.5.0\ext\ (I have created an  ext folder)
C:\Program Files\Java\test>java -classpath=. Log4jDemo
Unrecognized option: -classpath=.
Could not create the Java virtual machine.
Problem still persists.

I have pasted Important env vars from set below:

C:\Program Files\Java\test>set
CLASSPATH=C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar
HOMEPATH=\
HOMESHARE=\\pun-home-01\rkondapa$
INCLUDE=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
JAVA_HOME=C:\Program Files\Java\j2sdk1.5.0
JDK_DIR=C:\Program Files\Java\j2sdk1.5.0
LIB=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32
;C:\WINDOWS;C:\PROGRA~1\VANTIV~1.5;C:\Program Files\Microsoft SQL Server\80\Tool
s\BINN;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system
32;C:\WINDOWS;C:\PROGRA~1\VANTIV~1.5;C:\Program Files\Microsoft SQL Server\80\To
ols\BINN;C:\Program Files\Java\j2sdk1.5.0\bin;C:\Program Files\Java\j2sdk1.5.0\l
ib

Please do suggest the other ways
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually make that:

java -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar";. Log4jDemo
Objects:
The follwing is working
java -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar";. Log4jDemo

I would like to set this in CLASSPATH rather than typin git every time.
Give me the general CLASSPATH setting..
Regards
RK
set CLASSPATH=%CLASSPATH%;"C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar"
C:\Program Files\Java\test>set CLASSPATH=CLASSPATH%;"C:\Program Files\Java\j2sd
k1.5.0\lib\log4j.jar"

C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logg
er
        at Log4jDemo.<clinit>(Log4jDemo.java:5)
Problem still there...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You do not have the . (dot) included...
. means look in the current directory - your newly compiled class is in the current directory so include the . in the classpath and try again.
I have set the CLASSPATH in Environment.
When i do set at cmd prompt it is showin gthe classpath

C:\>set
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\rkondapa\Application Data
CLASSPATH=%CLASSPATH%;"C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar";.
but whe i run the program
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logg
er
        at Log4jDemo.<clinit>(Log4jDemo.java:5)
problem occurs again
if u didn't previousl;y have a class then just set it to:

CLASSPATH="C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar";.
You need to open a new cmd prompt for the changes to take effect.

Ok. It is resolved
i set in env CLASSPATH as
C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar;.
Thanks
woww - that is windows for you. Makes life so tough. In some places it expects quotes - in others it does not.

Phew - this was so simple but took unusually long.
>>C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar

Don't put your own or 3rd party jars in there - it's not the place for them. It's also unnecessary to add the JDK's own directories to your CLASSPATH

>>
I tried the following options:
...

4).
Copy the log4j.jar under C:\Program Files\Java\j2sdk1.5.0\ext\
>>

That's not the right place either. See my previous comment for the correct one
I am facing the same problem with the configuration of the log4j for my application in netbeans.
I added CLASSPATH as "C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar;." (without quotes ofcourse!)
there is classpath also which points to the jdk's lib.its been set properly.
the netbeans throwing the following error -
C:\Documents and Settings\pritam\Desktop\au\com\allhomes\common\util\Loggers.java:3: package org.apache.log4j does not exist
import org.apache.log4j.Logger;

Plz explain where I am  going wrong...?
plz post on this ,since i am trying this since two days and such a small issue...
why this works in very slow way????
No replies yet?is this going to be work or not..//???