Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Log4j issue

Status
Not open for further replies.

rk0000

Technical User
Mar 15, 2002
33
IN
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?
please do help in resolving this ....
Regards,
Rk

 
Because you need to place the log4j.jar into the classpath when running your program (just like you did when you compiled it).
Code:
java -cp "C:\Program Files\Java\j2sdk1.5.0\lib\log4j.jar" Log4jDemo.java

Tim
 
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
 
The first of the above would probably have worked had you placed the quotes around the path as in my suggestion.

Tim
 
Thanks a lot...
It is working with quotes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top