aspose file tools*
The moose likes Beginning Java and the fly likes List the files of a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List the files of a jar" Watch "List the files of a jar" New topic
Author

List the files of a jar

MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Hi all,

I need some help.

I have a jar that contains many package and files and i am accessing that file in an application .In the jar files there is some files in a perticular folder that means, com.org.twinkle.Test
I need to get all the Class files in this folder to my application
I tried several way but can't get the result.
Can anybody help me.

Regards
minu
[ October 24, 2005: Message edited by: minu su ]

God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42629
    
  65
Which ways of accessing a jar file have you tried?

The java.util.jar.JarFile class would seem to be the easiest. The entries() method allows you to iterate over all files, while getJarEntry() lets you access a specific file. getInputStream() then lets you read the contents of that file.


Ping & DNS - my free Android networking tools app
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What exactly do you plan on doing with these class files? Unless you are dong some advanced bytecode manipulation or instrumentation, you probably don't need to access the class files directly.

Also what have you tried so far? And how do the results differ from what you want? Please provide more details so we can help you.

Layne


Java API Documentation
The Java Tutorial
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

If you're trying to dynamically load the .class files from the jar, you will need to use a ClassLoader (probably a URLClassLoader) pointed to that jar file. But that's not beginner's stuff.

Echoing the sentiments of others, if you can provide more information on what you are trying to do, we can be more helpful in our responses.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Kelly Loyd
Greenhorn

Joined: Oct 10, 2005
Posts: 28
Hi,

Not sure if this is what you are looking for, but how about this command

jar -tvf <jarfile>.jar

Will show you everything that is contained in a jar file.

example (using a bit of the javaranch jar file )


Change the forward slash (/) to a dot (.) to get the fully qualified class name.

So one of the classes in this jar is com.javaranch.common.ADate

Hope this helps,

Kelly
[ October 24, 2005: Message edited by: Kelly Loyd ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Since you are talking about class files, another possibility is to use the Reflection API to list all the classes in a particular PACKAGE. Although packages are usually represented as folders in the underlying file system, they are conceptually different. Using the Reflection API would make sure you are loading valid classes rather than manipulating files directly.

Again, this is just a guess. We need more information in order to answer your question. What are you trying to do that requires you to access these files inside of the JAR?

As you can see, the purpose behind what you want to do may dictate how to go about doing it since there are several possibilities.

Layne
[ October 24, 2005: Message edited by: Layne Lund ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Since you are talking about class files, another possibility is to use the Reflection API to list all the classes in a particular PACKAGE.

If you've got a good way to do that, there are many people who would be interested in that

The problem is, technically speaking, classes in a package could exist in many different places and be loaded through different Class Loaders.
For example, if you have the files C:\Java\com\infotech\reports\TPSReport.class and X:\Shared\CoverSheets\com\infotech\reports\TPSCoverSheet.class, both the TPSReport and the TPSCoverSheet classes are in the com.infotech.reports package, but you could have two different class loaders; one with a path of C:\Java and one with X:\Shared\CoverSheets.

This makes Reflection particularly unhelpful, since there is no method to scan all classpaths to locate and list all classes in a particular package.
What do you do? You drop down to the file system(s) and/or jar files and iterate over the files (loading each one via Class.forName(), granted) -- which takes us back toward the original poster's question.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Joel McNary:
Since you are talking about class files, another possibility is to use the Reflection API to list all the classes in a particular PACKAGE.

If you've got a good way to do that, there are many people who would be interested in that

The problem is, technically speaking, classes in a package could exist in many different places and be loaded through different Class Loaders.
For example, if you have the files C:\Java\com\infotech\reports\TPSReport.class and X:\Shared\CoverSheets\com\infotech\reports\TPSCoverSheet.class, both the TPSReport and the TPSCoverSheet classes are in the com.infotech.reports package, but you could have two different class loaders; one with a path of C:\Java and one with X:\Shared\CoverSheets.

This makes Reflection particularly unhelpful, since there is no method to scan all classpaths to locate and list all classes in a particular package.
What do you do? You drop down to the file system(s) and/or jar files and iterate over the files (loading each one via Class.forName(), granted) -- which takes us back toward the original poster's question.


Thanks for the clarification. This even illustrates my main point even more: we are only guessing how to help the OP until we know the context and purpose of doing this.

Layne
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

If you've got a good way to do that, there are many people who would be interested in that


easy peasey
java -javagent
[ October 25, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
 
 
subject: List the files of a jar