Skip to main content

classpath browser

5 replies [Last post]
b09d4n
Offline
Joined: 2006-12-18

/**
* I'm just learning Java and I have problems with classes that I belive should be in the
* classpath, but my classes are't actually there.
*/
I would like a tool that:
Input: a package/class name (could use wildcards ?, * or -more complex- regular expresion);
Operations: parses the directories in the classpath variable, searching for that package/class name in files / file names;
Output: the full path of the executed file;
optional Output: it should display all packages/classes that have the same name, their full path, and highlight the one that is actually executed.
QUESTION #1: Is there such a tool - "Classpath browser"?
QUESTION #2: Where should I look for help to build such a tool (how to parse the classpath)?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aguther
Offline
Joined: 2003-07-30

As far as I can see the posted class can only list classes found within jars and classes on the top of the package level if on file system. There are some modifications needed for the part that analyzes the classpath directory based entries.

b09d4n
Offline
Joined: 2006-12-18

Yes, I replied my own question!

This helps a lot:
< Find occurrences of a class in your classpath or any ClassLoader>>

https://which4j.dev.java.net/ or
http://which4j.dev.java.net/

/* Btw this also helped me find the Linux command that does the same thing - <>.
* How lovely!
*/

and this is for more advanced tools
<>

http://classworlds.codehaus.org/

I''ve been searching for this tool for about a month. I belive it should be included with every JDK as Classpath is very stressful for beginners like me.

srinivas_j2ee
Offline
Joined: 2005-12-21

which4j can tell you the location of the given fully qualified class name, but not the list of class names in the classpath. I have the below code for that..

package com.forums.help;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
*/
public class ListClasses {

private static boolean debug = false;

/**
* Main method used in command-line mode for searching the system
* classpath for all the .class file available in the classpath
*
* @param args command-line arguments
*/
public static void main(String[] args) {
if (args.length ==1) {
if (args[0].equals("-debug")) {
debug = true;
System.out.println(listClasses().size());
} else {
System.err.println(
"error: unrecognized \"" + args[0] + "\" option ");
System.exit(-1);
}
} else if (args.length == 1 && args[0].equals("-help")) {
usage();
} else if (args.length == 0) {
System.out.println(listClasses().size());
} else {
usage();
}
System.exit(0);
}
private static void usage() {
System.err.println(
"usage: java ListClasses [-debug]"
+ "\n\tThe commandline version of ListClasses will search the system"
+ "\n\tclasspath defined in your environment for all .class files available" );
System.exit(-1);
}

/**
* Iterate over the system classpath defined by "java.class.path" searching
* for all .class files available
*
*/
private static List listClasses() {
List classes = new Vector(10,10);
try {
// get the system classpath
String classpath = System.getProperty("java.class.path", "");

if (classpath.equals("")) {
System.err.println("error: classpath is not set");
}

if (debug) {
System.out.println("system classpath = " + classpath);
}

StringTokenizer st =
new StringTokenizer(classpath, File.pathSeparator);

while (st.hasMoreTokens()) {
String token = st.nextToken();
File classpathElement = new File(token);
classes .addAll(classpathElement.isDirectory()
? loadClassesFromDir(classpathElement.list(new CLASSFilter()))
: loadClassesFromJar(classpathElement));
}
} catch (Exception e) {
e.printStackTrace();
}
return classes;
}

private static List loadClassesFromJar(File jarFile) {
List files = new Vector(10,10);
try {
if(jarFile.getName().endsWith(".jar")){
System.out.println(jarFile+" is being scanned");
Enumeration fileNames;
fileNames = new JarFile(jarFile).entries();
JarEntry entry = null;
while(fileNames.hasMoreElements()){
entry = fileNames.nextElement();
if(entry.getName().endsWith(".class")){
files.add(entry.getName());
}
}
}
} catch (IOException e) {

e.printStackTrace();
}

return files;
}

private static List loadClassesFromDir(String fileNames[]) {
return Arrays.asList(fileNames);
}
}
class CLASSFilter implements FilenameFilter{
public boolean accept(File dir, String name) {
return (name.endsWith(".class"));
}
}

krisdover
Offline
Joined: 2007-04-14

I am working on a similar problem but within Tomcat where i want to be able to retrieve a list of classes in the classpath to facilitate dynamic application extension. System.getProperty("java.class.path", "") does not return the correct class path. I have however had better luck with Thread.currentThread().getContextClassLoader().getResources("") however it does not seem to return the jars.

I have also found that servletConfig.getServletContext().getResourcePaths("/WEB-INF/lib") gives me jars but I want a system independent way of doing this since the code won't always be in the context of a servlet.

What I am hoping to achieve is a callback mechanism where I can retrieve a list of classes in the classpath which implement a particular interface, on demand.

So far I have this working within the context of the servlet but i also need it to work for code running as part of the container.

krisdover
Offline
Joined: 2007-04-14

Ok, subsequent to my last post i found this http://www.java-tips.org/java-se-tips/java.lang/how-to-print-classpath.html which I have modified to work like this:

java.net.URL [] urls = ((java.net.URLClassLoader)Thread.currentThread().getContextClassLoader()).getURLs();

And it gives me the jars and class directories!