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("Log4jDem o");
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.NoClassDefFoundE rror: org/apache/log4j/Logger
at Log4jDemo.<clinit>(Log4jDe mo.java:5)
Why this error is coming?
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("Log4jDem
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"
I am able to compile file by
C:\Program Files\Java\test>javac -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\
But when i do
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundE
at Log4jDemo.<clinit>(Log4jDe
Why this error is coming?
run it likk java -classpath C:\Program Files\Java\j2sdk1.5.0\lib\ log4j.jar Log4jDemo
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:\P rogram Files\Java\j2sdk1.5.0\lib\ log4j.jar
Then run yyour program as java Log4jDemo
set CLASSPATH=%CLASSPATH%;C:\P
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.
So you have 3 options:
1. set CLASSPATH=.;C:\Program Files\Java\j2sdk1.5.0\lib\
and then: java Log4jDemo
2. java -classpath=.;C:\Program Files\Java\j2sdk1.5.0\lib\
3. Copy the log4j.jar under C:\Program Files\Java\j2sdk1.5.0\ext\
java -classpath=. Log4jDemo.
If you intend to follow good practice, then include this class (the Log4Demo) as under a package.
Hope that helps.
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
>>Please stop repeating comments from other experts
And which one would that be?
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
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.NoClassDefFoundE rror: 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:\P rogram Files\Java\j2sdk1.5.0\lib\ log4j.jar
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundE rror: 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\rk ondapa$
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:\WI NDOWS\syst em32
;C:\WINDOWS;C:\PROGRA~1\VA NTIV~1.5;C :\Program Files\Microsoft SQL Server\80\Tool
s\BINN;C:\WINDOWS\system32 ;C:\WINDOW S;C:\WINDO WS\System3 2\Wbem;C:\ WINDOWS\sy stem
32;C:\WINDOWS;C:\PROGRA~1\ VANTIV~1.5 ;C:\Progra m 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
1).
C:\Program Files\Java\test>java -classpath C:\Program Files\Java\j2sdk1.5.0\lib\
Exception in thread "main" java.lang.NoClassDefFoundE
Problem still persists
2).
Set the Classpath first
C:\Program Files\Java\test>set CLASSPATH=%CLASSPATH%;C:\P
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundE
Problem still persists
3).
C:\Program Files\Java\test>java -classpath=.;C:\Program Files\Java\j2sdk1.5.0\lib\
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\
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\
HOMEPATH=\
HOMESHARE=\\pun-home-01\rk
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
;C:\WINDOWS;C:\PROGRA~1\VA
s\BINN;C:\WINDOWS\system32
32;C:\WINDOWS;C:\PROGRA~1\
ols\BINN;C:\Program Files\Java\j2sdk1.5.0\bin;
ib
Please do suggest the other ways
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
actually make that:
java -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\ log4j.jar" ;. Log4jDemo
java -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\
ASKER
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
The follwing is working
java -classpath "C:\Program Files\Java\j2sdk1.5.0\lib\
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"
ASKER
C:\Program Files\Java\test>set CLASSPATH=CLASSPATH%;"C:\P rogram Files\Java\j2sd
k1.5.0\lib\log4j.jar"
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundE rror: org/apache/log4j/Logg
er
at Log4jDemo.<clinit>(Log4jDe mo.java:5)
Problem still there...
k1.5.0\lib\log4j.jar"
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundE
er
at Log4jDemo.<clinit>(Log4jDe
Problem still there...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
. means look in the current directory - your newly compiled class is in the current directory so include the . in the classpath and try again.
ASKER
I have set the CLASSPATH in Environment.
When i do set at cmd prompt it is showin gthe classpath
C:\>set
ALLUSERSPROFILE=C:\Documen ts and Settings\All Users
APPDATA=C:\Documents and Settings\rkondapa\Applicat ion 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.NoClassDefFoundE rror: org/apache/log4j/Logg
er
at Log4jDemo.<clinit>(Log4jDe mo.java:5)
problem occurs again
When i do set at cmd prompt it is showin gthe classpath
C:\>set
ALLUSERSPROFILE=C:\Documen
APPDATA=C:\Documents and Settings\rkondapa\Applicat
CLASSPATH=%CLASSPATH%;"C:\
but whe i run the program
C:\Program Files\Java\test>java Log4jDemo
Exception in thread "main" java.lang.NoClassDefFoundE
er
at Log4jDemo.<clinit>(Log4jDe
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" ;.
CLASSPATH="C:\Program Files\Java\j2sdk1.5.0\lib\
You need to open a new cmd prompt for the changes to take effect.
ASKER
Ok. It is resolved
i set in env CLASSPATH as
C:\Program Files\Java\j2sdk1.5.0\lib\ log4j.jar; .
Thanks
i set in env CLASSPATH as
C:\Program Files\Java\j2sdk1.5.0\lib\
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.
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
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\allho mes\common \util\Logg ers.java:3 : package org.apache.log4j does not exist
import org.apache.log4j.Logger;
Plz explain where I am going wrong...?
I added CLASSPATH as "C:\Program Files\Java\j2sdk1.5.0\lib\
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
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..//???
No replies yet?is this going to be work or not..//???