Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding classes

0 views
Skip to first unread message

Dan Peder Eriksen

unread,
Nov 17, 2002, 1:33:21 PM11/17/02
to
Hi!

In a jar file is it possible to find the classes in it that implements a
certain interface?
If so, how?

Dan


Avi Abrami

unread,
Nov 17, 2002, 8:56:31 PM11/17/02
to
Dan Peder Eriksen wrote:
>
> In a jar file is it possible to find the classes in it that
> implement a certain interface?
> If so, how?
>

Hi Dan,
I'm assuming your JAR file contains compiled java byte code
files (".class" files), and that you are using at least
java version 1.2.

If you want to do this programatically (i.e. you want to
write java code that does this), then you could do it using
the classes in the "java.util.jar" package to read the
contents of the JAR file, and then use the method
"getInterfaces()" (in class "java.lang.Class") to see what
interfaces the class implements. Please see the javadocs
for more details.

If you want to do it using tools/utilities, then use the
"jar" tool to extract the contents of the JAR file, and
then use the "javap" tool to see what interfaces are
implemented by each class. Both these tools are part of
the JDK and have accompanying documentation explaining
how to use them.

There are also third-party tools for viewing the contents
of jar files, including the following:

http://www.jarspy.org/

http://snieda.50g.com/_framed/50g/snieda/doc/index.html

Hope this answers your question.

Good Luck,
Avi.

Dan Peder Eriksen

unread,
Nov 17, 2002, 11:46:37 PM11/17/02
to

"Avi Abrami" <abr...@yahoo.com> skrev i melding
news:3DD848CF...@yahoo.com...

Thanks for your help! But I have run into a problem when trying what your
said:

I get the following error:
java.lang.InstantiationException
at
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Instan
tiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:306)
at java.lang.Class.newInstance(Class.java:259)
at org.jctm.gui.dialog.TourneyDialog.<init>(TourneyDialog.java:45)
at org.jctm.gui.CTMFrame.<init>(CTMFrame.java:50)
at jCTM.<init>(jCTM.java:31)
at jCTM.main(jCTM.java:41)

When trying to make an instance of org.jctm.Berger.
From javadoc: "Thrown when an application tries to
create an instance of a class using the newInstance
method in class Class, but the specified class object
cannot be instantiated because it is an interface or is
an abstract class"

But org.jctm.Berger isn't an interface or abstract class.

Code(where try to make an instance, TourneyDialog.java):
JarInputStream jis = new JarInputStream(new FileInputStream("jCTM.jar"));
JarEntry je;
while((je = jis.getNextJarEntry()) != null) {
String name = je.toString();
if(name.indexOf(".class") != -1) {
name = name.replace('/', '.');
name = name.substring(0, name.length()-6);
Class c = Class.forName(name);
Class sc = c.getSuperclass();
if(sc != null && sc.getName().equals("org.jctm.Manager")) {
System.out.println(c.getName());
Object o = c.newInstance();
//list.add(m.getName());
}
}
}

Berger.java(Manager is an abstract class):

public abstract class Berger extends Manager {
protected boolean[] white;
protected ArrayList playerQue;

public Berger(Tourney tourney) {
super(tourney);
playerQue = new ArrayList();
}

public Berger() {}

and so on............
}

Avi Abrami

unread,
Nov 18, 2002, 6:28:03 PM11/18/02
to

Dan,
If the above really is your code, then "Berger" is an
abstract class -- so you can't instantiate it!

Good Luck,
Avi.

0 new messages