Getting package info from JAR file

L

laredotornado

Hi,

I'm on Win XP using Java 7 but I have access to Unix tools using
Cygwin. I was wondering, short of unzipping a JAR file and searching
through it, if there's a way to figure out the package to which a
class belongs, assuming I know the class name and that the JAR file
definitely includes a class by this name.

Thanks, - Dave
 
M

markspace

Hi,

I'm on Win XP using Java 7 but I have access to Unix tools using
Cygwin. I was wondering, short of unzipping a JAR file and searching
through it, if there's a way to figure out the package to which a
class belongs, assuming I know the class name and that the JAR file
definitely includes a class by this name.


Not really, no. If you don't know the package name, the class could
belong to any package. What package does the annotation @ManagedBean
belong too? Well, on my system, there are three ManagedBeans which
belong to different packages, so which one did you want?

You've got to have the package name or you don't know which class it is.
 
J

John B. Matthews

Not really, no. If you don't know the package name, the class could
belong to any package. What package does the annotation @ManagedBean
belong too? Well, on my system, there are three ManagedBeans which
belong to different packages, so which one did you want?

You've got to have the package name or you don't know which class it
is.

You can narrow down the list of matching simple names like this:

$ jar tf src.jar | grep "/Timer\."
src/java/util/Timer.java
src/javax/management/timer/Timer.java
src/javax/swing/Timer.java

And expand the search using find:

$ find . -name \*.jar | xargs -I % jar tf % | grep "/Timer\."
doc/api/java/util/Timer.html
doc/api/java/util/class-use/Timer.html
doc/api/javax/management/timer/Timer.html
doc/api/javax/management/timer/class-use/Timer.html
doc/api/javax/swing/Timer.html
doc/api/javax/swing/class-use/Timer.html
src/java/util/Timer.java
src/javax/management/timer/Timer.java
src/javax/swing/Timer.java
 
A

Arne Vajhøj

I'm on Win XP using Java 7 but I have access to Unix tools using
Cygwin. I was wondering, short of unzipping a JAR file and searching
through it, if there's a way to figure out the package to which a
class belongs, assuming I know the class name and that the JAR file
definitely includes a class by this name.

jar tvf something.jar | grep Foobar

will find all occurences of Foobar class in jar - note that
there can be more than one!

Programmatic it would be:

JarFile jf = new JarFile(args[0]);
Enumeration e = jf.entries();
while(e.hasMoreElements()) {
JarEntry je = (JarEntry)e.nextElement();
String clsnm = je.getName().replace('/', '.');
if(clsnm.endsWith(".class")) {
// you have a clsnm
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top