Unable to access classes in a jar file

I have a folder called src. In this folder i have a jar file log4j-1.2.7 which contains packages. In the same folder src i have another package com.abc which has a file App.java
When i compile this class it cannot access classes within the jar file.
Please help
sandy19Asked:
Who is Participating?
 
JugglerWConnect With a Mentor Commented:
Try this:

D:\>src\classes>javac -classpath ..\log4j-1.2.7.jar com\abc\App.java
0
 
JugglerWCommented:
Tell the compiler where jar files are. E.g. if your working dir is above src:

javac -classpath src/log4j-1.2.7.jar com/abc/App.java

Also note that the location of your App.java should mirror the package structure.
0
 
JugglerWCommented:
So if your package is com.abc the App.java sholud be here: src\com\abc\App.java
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
sandy19Author Commented:
structure is something like this

src\log4j-1.2.7.jar

and

src\classes\com\abc\App.java

I am writing
D:\>src\classes>javac com\abc\App.java

It's unable to find the classes in jar file
0
 
oraelbisCommented:
Typical structure for basic java project is
/Project
+-- /classes
+-- /lib
+-- /src

put your library in lib folder.
Your file App.java may include package identifier as first string
package com.abc;

For compile it use something like:
javac.exe -classpath D:\j\Project\lib\log4j.jar -sourcepath D:\j\Project\src -target 1.2 -g -d D:\j\Project\classes D:\j\Project\src\com\abc\App.java



0
 
oraelbisCommented:
To start it use :
java.exe -classpath D:\j\Project\classes;D:\j\Project\lib\log4j.jar com.abc.App
0
 
sandy19Author Commented:
thanks a lot it works fine using

D:\>src\classes>javac -classpath ..\log4j-1.2.7.jar com\abc\App.java

But even now i can't run it.
It gives the follwoing error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logg
er
        at com.abc.App.<clinit>(App.java:37)
0
 
JugglerWCommented:
Run it using:

D:\>src\classes>java -classpath .;..\log4j-1.2.7.jar com\abc\App.java

Note the .; ! Its for: Using class files in current directory.

But for future projects follow the hints of  oraelbis for structuring.
0
 
JugglerWCommented:
Sorry correction:

D:\>src\classes>java -classpath .;..\log4j-1.2.7.jar com.abc.App

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.