Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List the files of a jar

 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kelly Loyd
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic