| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Comparator and Reflection using Java
Description :
Java and CompareTo More...
|
Written By : Amit
Title :
Composite keys in Hashtable
Description :
key and value pair More...
|
Written By : Amit
Title :
Customizing JTree using Swing
Description :
AWT using Java Technology More...
|
Written By : Amit
Title :
Event handling and Java Technology
Description :
Event handling and Java Technology More...
|
Written By : admin
Title :
Using Final Keyword
Description :
Example on using Final Keyword in Java code More...
|
| Tags/Keywords : Using-Java-Technology - Search-Class-in-JAR - Coding-Tips-on-Java Example Author : Amit Date (Year/Month/Date): 2009-01-26
Class file search in Jar file using Java Technology | | | | |
|
How to search for a class
file from a bunch of Jar
files in a directory file
system?
I have created some of
simple to use tools using
Java Technology for making
developer work simpler in
some form or the other.
While working on some
assignment of adding a
required JAR file from a
list of many Jar files from
a directory, so as to be able
to overcome a class not found
exception, by placing the
required Jar file to classpath
environment variable. So next
step is to find the needed Jar
file from so many Jar files is
a tough job, and neither did
I want to put all the Jar
files in classpath nor to
open each and every Jar file
to see if the required class
file is present or not. Then
I realize the need of a piece
of code that does this job
for me by going through each
and every Jar files in the
directory and looking inside
it for the Class file I wanted
it to search. Once found just
shows me the Jar file and stop
searching further.
|
Java Technology has a package of classes from
java.util.zip and java.util.jar to deal with
Zip and Jar files entries. This gave me
an idea of writing this class to look for this
objective.
Step includes:
1. Take a directory path and class file name as input.
2. List all files available in that directory.
3. Iterate through the list of files and check
if the file is a Jar file or not.
4. If a Jar file, then create an
java.util.jar.JarFile object and
enumerate through the entries (here entries are
the class files).
5. For each entry (which is nothing but
java.util.zip.ZipEntry),
check if the toString returns a string equivalent to the class
file name.
6. If yes, just return from the iteration and
print the jar file name on console.
While writing code, step 1 has following code:
public TestJar(String path, String classToSearch)
directory path and class to search as arguments
to constructor.
step 2:
java.io.File f = new java.io.File(path);
java.io.File[] files=f.listFiles();
create a File object for the path supplied, and list
all files in the directory as listFiles.
step 3:
for(int i=0;i<files.length;i++)
{
if(isJarFile(files[i])){
isJarFile is a method to check whether the file name
it takes as argument is a Jar file or not.
public boolean isJarFile(File file)
{
StringTokenizer st=
new StringTokenizer(file.getName(),".");
String ext=null;
while(st.hasMoreTokens())
{
if( ((String)st.nextToken()).equals("jar"))
{
return true;
}
}
return false;
}
step 4:
fileName=files[i].getName();
JarFile zf=new JarFile(path+"/"+ fileName);
create a JarFile object with an argument as the directory
path with the file name.
step 5:
Enumeration e=zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze=(ZipEntry)e.nextElement();
Extract each ZipEntry inside the Jar file.
step 6:
if((ze.toString()).equals(classToSearch+".class")){
System.out.println("\nThe jar file name is
:"+path+"/"+fileName+" : "+ze);
System.exit(0);;
If class to search is same as the ZipEntry toString, then
show this Jar file and exit.
This is purely my imagination and user or reader should
take appropriate precaution in handling these kind of
code writing in a project or at work place (if interested).
Writer of this article never claims that this will work
or is risk free.
| |
 | Author of this article/writeup has expressed his/her willingness |
to help or guide users with any technical difficulties he/she faces
while working with the example code environment setting up, running
and resolving any such exception raised during compile or at runtime.
You may ask for any technical doubt or seek technical help related to
this article by using following form to reach for technical help
from the Author for FREE.
This article's Author shall be reading your request and responding
within reasonable time (no resolution timeframe defined as such).
|
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Coding Tips on Java >>> Search Class in JAR >>> Using Java Technology |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|