Solved

Unable to access classes in a jar file

Posted on 2004-08-06
9
1,052 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

808 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