Solved

Unable to access classes in a jar file

Posted on 2004-08-06
9
1,051 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 54
Printing to a specific printer tray (HP 806dn printer) 3 83
firstChar challenge 13 106
Windows 10 IE Certificate Issue 10 41
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

919 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

14 Experts available now in Live!

Get 1:1 Help Now