list all files on classpath

K

Kai Schlamp

Hello!

Is there a way to programmatically list all files or packages on the
classpath?

Best regards,
Kai
 
L

Lew

Kai said:
Is there a way to programmatically list all files or packages on the
classpath?

Sort of, but not really. Classpaths can include things that are not
files. Things can appear and disappear from the classpath at
different times. Classes can be available to load that are not on the
classpath. Different class loaders will have different classpaths,
and class loaders can come and go.

You can acquire a snapshot of a meaningful subset of resources
available via a meaningful subset of the classpaths available at a
given moment, if you can assume some restrictions like a file-system-
based classpath.
 
S

Stefan Ram

Kai Schlamp said:
Is there a way to programmatically list all files or packages
on the classpath?

All files:

public class Main
{ public static void main( final java.lang.String[] args )
throws java.lang.Throwable
{ final java.lang.String list =
java.lang.System.getProperty( "java.class.path" );
for( final java.lang.String path : list.split( ";" ))
{ final java.io.File object = new java.io.File( path );
if( object.isDirectory() )
for( java.lang.String entry : object.list() )
{ final java.io.File thing = new java.io.File( entry );
if( thing.isFile() )
java.lang.System.out.println( thing ); }
else if( object.isFile() )
java.lang.System.out.println( object ); }}}
 
R

Roedy Green

Is there a way to programmatically list all files or packages on the
classpath?


see http://mindprod.com/applet/wassup.html

It is a System property
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer
 
L

Lew

Stefan said:
Kai Schlamp said:
Is there a way to programmatically list all files or packages
on the classpath?

All files:

public class Main
{ public static void main( final java.lang.String[] args )
throws java.lang.Throwable
{ final java.lang.String list =
java.lang.System.getProperty( "java.class.path" );
for( final java.lang.String path : list.split( ";" ))
{ final java.io.File object = new java.io.File( path );
if( object.isDirectory() )
for( java.lang.String entry : object.list() )
{ final java.io.File thing = new java.io.File( entry );
if( thing.isFile() )
java.lang.System.out.println( thing ); }
else if( object.isFile() )
java.lang.System.out.println( object ); }}}

Would you please be so kind as to indent your listings on Usenet a wee bit
more conventionally? Your current layout is very difficult to read.

Also, there is no need for or point to the use of FQNs for the java.lang
package. Indeed, in this newsgroup you're really quite safe using simple
class names for java.io and java.util classes.
 
J

John B. Matthews

[QUOTE="Lew said:
Kai Schlamp said:
Is there a way to programmatically list all files or packages
on the classpath?

All files:

public class Main
{ public static void main( final java.lang.String[] args )
throws java.lang.Throwable
{ final java.lang.String list =
java.lang.System.getProperty( "java.class.path" );
for( final java.lang.String path : list.split( ";" ))
{ final java.io.File object = new java.io.File( path );
if( object.isDirectory() )
for( java.lang.String entry : object.list() )
{ final java.io.File thing = new java.io.File( entry );
if( thing.isFile() )
java.lang.System.out.println( thing ); }
else if( object.isFile() )
java.lang.System.out.println( object ); }}}

Would you please be so kind as to indent your listings on Usenet a
wee bit more conventionally? Your current layout is very difficult
to read.[/QUOTE]

Lew, I admire your indefatigable defense of style as an aid to clarity,
but I would miss Stefan's distinctive, functional style. IMO, his
examples are only slightly more difficult to read, and always worth
understanding.
Also, there is no need for or point to the use of FQNs for the
java.lang package. Indeed, in this newsgroup you're really quite
safe using simple class names for java.io and java.util classes.

I can't argue with those points. I revert to my own pedestrian style to
add a cross-platform path separator and recursive directory listing:

import java.io.File;

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

public static void main(final String[] args) throws Throwable {
final String pathSep = System.getProperty("path.separator");
final String list = System.getProperty("java.class.path");
for (final String path : list.split(pathSep)) {
final File object = new java.io.File(path);
if( object.isDirectory()) ls(object);
else System.out.println(object);
}
}
/** list recursively */
private static void ls(File f) {
File list[] = f.listFiles();
for (File file : list) {
if (file.isDirectory()) ls(file);
else System.out.println(file);
}
}
}
 
M

Mark Space

Lew said:
Would you please be so kind as to indent your listings on Usenet a wee
bit more conventionally? Your current layout is very difficult to read.

I think of Stefan's style as a compact notation especially for netnews.
It's a bit like a shar file. You have copy it into your favorites
IDE, press the "reformat" button, and see what you get. Everyone has a
different formatting preference anyway.
 
A

Arne Vajhøj

Mark said:
I think of Stefan's style as a compact notation especially for netnews.
It's a bit like a shar file. You have copy it into your favorites IDE,
press the "reformat" button, and see what you get. Everyone has a
different formatting preference anyway.

SUN has published a coding convention. I know that format is default
in Eclipse. I would expect it to be default in NetBeans as well.

I would expect a huge portion of Java programmers to follow that
reasonable close.

Arne
 
D

Donkey Hottie

SUN has published a coding convention. I know that format is default
in Eclipse. I would expect it to be default in NetBeans as well.

I would expect a huge portion of Java programmers to follow that
reasonable close.

Arne

I have personal settings in both of those. The default looks ugly.
 
L

Lew

Donkey said:
I have personal settings in both of those. The default looks ugly.

A strength of conventions as opposed to regulations is that one can flout them
to a degree. Small variations, like the common one that places an opening
brace on its own line, tend to go unremarked or even appreciated. Large
deviations can brand one a troublemaker and lead to excoriation, even
shunning. Within the bounds of convention there is some latitude, for example
in the choice of width for an indent level. But you must indent, and gods
forfend you should wear white shoes after Labor Day!

The point of convention is to facilitate social interaction, e.g., to share
source-code listings in order to request or receive assistance. Flouting
convention yields at best no diminishment of that facility, or only a slight
diminishment, and at worst complete loss of engagement. Sure, you can cut off
your nose to spite your face, if you really think that serves your interest.
 
A

Arne Vajhøj

Lew said:
A strength of conventions as opposed to regulations is that one can
flout them to a degree. Small variations, like the common one that
places an opening brace on its own line, tend to go unremarked or even
appreciated. Large deviations can brand one a troublemaker and lead to
excoriation, even shunning. Within the bounds of convention there is
some latitude, for example in the choice of width for an indent level.

Or even smaller things.

I try to use the SUN coding convention, but a few things like a space
between cast right round bracket and subject does not fell natural to
me.

Arne
 
A

Andrew Thompson

Is there a way to programmatically list all files or packages on the
classpath?

A better question is "why do you want to know?". Or more to the
point, what program feature do you intend to offer the end user
by knowing that information?

If you answer that question, I bet there are better strategies
for achieving the goal, than trawling through the classpath.
 
T

Tom Anderson

A strength of conventions as opposed to regulations is that one can flout
them to a degree. Small variations, like the common one that places an
opening brace on its own line, tend to go unremarked or even appreciated.
Large deviations can brand one a troublemaker and lead to excoriation, even
shunning. Within the bounds of convention there is some latitude, for
example in the choice of width for an indent level.

And my historical habit of putting a space before semicolons:

for (int i = 0 ; i < N ; ++i) {
wash(i) ;
}

Although i've stopped doing even this, under the conformance-inducing
influence of pair programming.

Back in the day, i used to put spaces inside parens, and still see other
people doing this from time to time:

public void wash( int i ) {
if ( i > K ) {
washLarge( i );
}
else {
int j = ( i * 1000 ) + OFFSET;
washSmall( j );
}
}

Stefan's idiolect goes further, and is bordering on the incomprehensible.
The thing i personally find most annoying about it is the use of
fully-qualified names, even for things in java.lang, which just adds
visual noise for no additional semantic content. His tendency to couch
solutions in terms of his own personal library classes is also a barrier
to understanding at times - although it's publicly accessible, and
captures widespread CS notions, so it's quite possible to follow.

But still, his ideas and implementations are good, so moaning about the
formatting seems churlish, really.

tom
 
T

Tom Anderson

Or even smaller things.

I try to use the SUN coding convention, but a few things like a space
between cast right round bracket and subject does not fell natural to
me.

Yes, i find that incredibly annoying, and don't do it.

tom
 
A

Arne Vajhøj

Kai said:
Is there a way to programmatically list all files or packages on the
classpath?

See the code below for some inspiration.

Arne

========================================

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

public class ClassFind {
public interface CallBack {
public boolean match(String clznam, String src);
public void process(String clznam, String src);
}

private void foundFile(String basedirnam, String fnm, CallBack cb) {
String clznam = fnm.substring(basedirnam.length()).replace('/',
'.').replace(".class", "");
if(cb.match(clznam, fnm)) {
cb.process(clznam, fnm);
}
}
private void findDir(String basedirnam, String dirnam, CallBack cb) {
File dir = new File(dirnam);
String[] dirlist = dir.list();
for(int i = 0; i < dirlist.length; i++) {
String fnm = dirnam + dirlist;
if ((new File(fnm)).isDirectory()) {
findDir(basedirnam, fnm + "/", cb);
} else if(fnm.endsWith(".class")) {
foundFile(basedirnam, fnm, cb);
}
}
}
private void foundJar(String jarnam, String clznam, CallBack cb) {
if(cb.match(clznam, jarnam)) {
cb.process(clznam, jarnam);
}
}
private void findJar(String jarnam, CallBack cb) {
try {
JarFile jf = new JarFile(jarnam);
Enumeration<JarEntry> e = jf.entries();
while(e.hasMoreElements()) {
JarEntry je = e.nextElement();
String clznam = je.getName().replace('/', '.');
if(clznam.endsWith(".class")) {
foundJar(jarnam, clznam, cb);
}
}
} catch (IOException e) {
}
}
private void find(URL url, CallBack cb) {
if(url.getProtocol().equals("file")) {
String fnm = url.toExternalForm().replace("file:/", "");
File f = new File(fnm);
if(f.isDirectory()) {
findDir(fnm, fnm, cb);
} else {
findJar(fnm, cb);
}

}
}
public void find(CallBack cb) {
ClassLoader cl = ClassFind.class.getClassLoader();
while(cl != null) {
if(cl instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader)cl).getURLs();
for(int i = 0; i < urls.length; i++) {
find(urls, cb);
}
}
cl = cl.getParent();
}
}
public static void main(String[] args) {
ClassFind cf = new ClassFind();
cf.find(new ClassFind.CallBack() {
public boolean match(String clznam, String src) {
return clznam.startsWith("dk.");
}
public void process(String clznam, String src) {
System.out.println(clznam + " in " + src);
}});
}
}
 

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,596
Members
45,143
Latest member
SterlingLa
Top