Solved

Unable to access classes in a jar file

Posted on 2004-08-06
9
1,050 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
  • 5
  • 2
  • 2
9 Comments
 
LVL 7

Expert Comment

by:JugglerW
Comment Utility
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
Comment Utility
So if your package is com.abc the App.java sholud be here: src\com\abc\App.java
0
 

Author Comment

by:sandy19
Comment Utility
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
 
LVL 7

Accepted Solution

by:
JugglerW earned 100 total points
Comment Utility
Try this:

D:\>src\classes>javac -classpath ..\log4j-1.2.7.jar com\abc\App.java
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:oraelbis
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry correction:

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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now