How do I search for a constant in a class in a bunch of Jars?

L

laredotornado

Hi,

I'm using Mac OS X (bash shell) with Java 1.5. I have an enum class
and I think an old version is drifting around in a JAR file somewhere
that contains a field, "MATCH_999". Does anyone know how to search
through the JAR files to find the JAR(s) that may contain this? I
would prefer not to unzip each one.

Thanks for any advice, - Dave
 
A

Andreas Leitgeb

laredotornado said:
I'm using Mac OS X (bash shell) with Java 1.5. I have an enum class
and I think an old version is drifting around in a JAR file somewhere
that contains a field, "MATCH_999". Does anyone know how to search
through the JAR files to find the JAR(s) that may contain this? I
would prefer not to unzip each one.

Giving your compiler the -verbose option will make it write for each
class used, where it found it. Perhaps that way you find it.

It did help me recently in a similar situation.
 
L

laredotornado

Giving your compiler the -verbose option will make it write for each
class used, where it found it. Perhaps that way you find it.

It did help me recently in a similar situation.

The problems I'm running into are occurring during run time -- the
wrong version of a class is getting loaded. Thus I want to see if
that class (and specifically I know what I'm looking for in that
class) exists in different JARs.

I can recompile my project, but the bad JAR file is still going to be
somewhere in the classpath.

Any advice on finding which JAR contains the offending class file is
appreciated. Thanks, - Dave
 
J

Jim

Hi,

I'm using Mac OS X (bash shell) with Java 1.5.  I have an enum class
and I think an old version is drifting around in a JAR file somewhere
that contains a field, "MATCH_999".  Does anyone know how to search
through the JAR files to find the JAR(s) that may contain this?  I
would prefer not to unzip each one.

Thanks for any advice, - Dave

If you're comfortable with a little shell scripting you can do
something like the following (example looks for which class
getAsciiStream is found in):

prompt$ for f in mysql-connector-java.jar; do
echo "$f"
jar tf "$f" \
| egrep '\.class$' \
| tr '/' '.' \
| while read classname; do
echo " $classname"
javap -classpath "$f" ${classname%.class} \
| egrep getAsciiStream
done
done
mysql-connector-java.jar
com.mysql.jdbc.AssertionFailedException.class
com.mysql.jdbc.Blob.class
com.mysql.jdbc.Buffer.class
com.mysql.jdbc.CharsetMapping.class
com.mysql.jdbc.Clob.class
public java.io.InputStream getAsciiStream();
com.mysql.jdbc.Connection$UltraDevWorkAround.class
com.mysql.jdbc.Connection.class
com.mysql.jdbc.DatabaseMetaData.class
[...etc...]
 
J

John B. Matthews

laredotornado said:
I'm using Mac OS X (bash shell) with Java 1.5. I have an enum class
and I think an old version is drifting around in a JAR file somewhere
that contains a field, "MATCH_999". Does anyone know how to search
through the JAR files to find the JAR(s) that may contain this? I
would prefer not to unzip each one.

Using the -verbose option of the `java` command may shed light.
Alternatively, the example below may be modified to search particular
paths. As written, it examines the java.class.path property, but an
errant $CLASSPATH environment variable is another suspect. Searching the
individual JarEntry's InputStream should be a straightforward extension.

<code>
package cli;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/** @author Ram, Matthews */
public class ShowClassPath {

public static void main(final String[] args) throws Throwable {
final String pathSep = File.pathSeparator;
final String list = System.getProperty("java.class.path");
for (final String path : list.split(pathSep)) {
final File f = new java.io.File(path);
if (f.isDirectory()) {
ls(f);
} else if (f.toString().endsWith("jar")) {
lsJar(f);
} else {
System.out.println(f);
}
}
}

private static void ls(File f) {
File[] list = f.listFiles();
for (File file : list) {
if (file.isDirectory()) {
ls(file);
} else {
System.out.println(file);
}
}
}

private static void lsJar(File f) {
try {
JarFile jar = new JarFile(f);
Enumeration e = jar.entries();
while (e.hasMoreElements()) {
JarEntry je = (JarEntry) e.nextElement();
System.out.println(je.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
</code>
 
A

Alessio Stalla

Hi,

I'm using Mac OS X (bash shell) with Java 1.5.  I have an enum class
and I think an old version is drifting around in a JAR file somewhere
that contains a field, "MATCH_999".  Does anyone know how to search
through the JAR files to find the JAR(s) that may contain this?  I
would prefer not to unzip each one.

Thanks for any advice, - Dave

If you know that the field is called MATCH_999, it means you have a
reference to the enum. So you can print the result of:

YourEnum.class.getResource("YourEnum.class")

it should be a URL containing the full path of the Jar file containing
the enum.

hth,
Alessio Stalla
 
J

John B. Matthews

laredotornado said:
The problems I'm running into are occurring during run time -- the
wrong version of a class is getting loaded. Thus I want to see if
that class (and specifically I know what I'm looking for in that
class) exists in different JARs.

I can recompile my project, but the bad JAR file is still going to be
somewhere in the classpath.

Any advice on finding which JAR contains the offending class file is
appreciated. Thanks, - Dave

Both `javac` and `java` have -verbose options. Depending on the problem,
one, the other, or a comparison of the two may be helpful.

Check that you have not incorrectly set the environment variable,
CLASSPATH. By fits and starts, I have come to believe that it's best not
to set it at all, relying instead on the corresponding compiler and
runtime options.
 
N

Nigel Wade

Hi,

I'm using Mac OS X (bash shell) with Java 1.5. I have an enum class and
I think an old version is drifting around in a JAR file somewhere that
contains a field, "MATCH_999". Does anyone know how to search through
the JAR files to find the JAR(s) that may contain this? I would prefer
not to unzip each one.

Thanks for any advice, - Dave

You could always try grep.

If you know the class name then just list the contents of the jars (jar
tf file.jar) and grep for the class name.

e.g.
find some_dir -name \*.jar | while read file; do
echo $file
jar tf $file| grep classname
done

should tell you every jar file which contain the class. This should
narrow the search.

Then try grep on the jar file itself, searching for the field. Whether
the field's actual name exists in the jar may depend on what debugging
information is included in the class files. I have only performed one
rudimentary test with grep, and searched for a static String identifier,
which did work.
 
R

Roedy Green

I'm using Mac OS X (bash shell) with Java 1.5. I have an enum class
and I think an old version is drifting around in a JAR file somewhere
that contains a field, "MATCH_999". Does anyone know how to search
through the JAR files to find the JAR(s) that may contain this? I
would prefer not to unzip each one.

I would do it on Windows by Winzip unpack then scan with Funduc
Search/Replace.

To do it with code you could use the Zip class to scan the jar, and
just read each class file as a string of bytes, then look for that
byte string (utf-8 encoding).

For a more general solution, you could decode the class file.
See http://mindprod.com/jgloss/javaclassformat.html
and follow the links for tools.
--
Roedy Green Canadian Mind Products
http://mindprod.com

I advocate that super programmers who can juggle vastly more complex balls than average guys can, should be banned, by management, from dragging the average crowd into system complexity zones where the whole team will start to drown.
~ Jan V.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top