Solved

Unable to access classes in a jar file

Posted on 2004-08-06
9
1,054 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:sandy19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 7

Expert Comment

by:JugglerW
ID: 11741923
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11741925
So if your package is com.abc the App.java sholud be here: src\com\abc\App.java
0
 

Author Comment

by:sandy19
ID: 11741944
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
JugglerW earned 100 total points
ID: 11742005
Try this:

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

Expert Comment

by:oraelbis
ID: 11742006
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
 
LVL 3

Expert Comment

by:oraelbis
ID: 11742013
To start it use :
java.exe -classpath D:\j\Project\classes;D:\j\Project\lib\log4j.jar com.abc.App
0
 

Author Comment

by:sandy19
ID: 11742036
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11742343
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11742345
Sorry correction:

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

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question