2004.04.29


com.jdc.sys
Class ClassPath

java.lang.Object
  |
  +--com.jdc.sys.ClassPath

public class ClassPath
extends Object

ClassPath provides a reference to the Java system classpath. This reference is essentially the same as SystemProperty class SystemProperty.CLASS_PATH constant.

However, this class provides methods to manipulate the system classpath including toArray() which returns the individual system classpath entries as a String[] array.

ClassPath may also be used to modify the system classpath at runtime. Since the system ClassLoader obtained using ClassLoader.getSystemClassLoader() is a subclass of URLClassLoader, it may be cast as a URLClassLoader and used as one.

URLClassLoader has a protected method URLClassLoader.addURL(URL), which may be used to add files, jars, web addresses or any valid URL.

Since the addURL method is protected, reflection is used to invoke it.

Reference: Modify Classpath At Runtime

Version:
1.0
Author:
Jay Damon
See Also:
SystemProperty.CLASS_PATH

Constructor Summary
ClassPath()
          Constructs a new ClassPath object.
 
Method Summary
 void addFile(File file)
          Adds the specified File to the system class path.
 void addFile(String fileName)
          Adds the specified file name to the system class path.
 void addURL(String urlName)
          Adds the specified URL name to the system class path.
 void addURL(URL url)
          Adds the specified URL to the system class path.
 String[] getAllPaths()
          Returns all possible Java class path directories as a String[] array in class path order.
 String[] getPaths()
          Returns the Java class path directories as a String[] array in class path order.
 String[] toArray()
          Returns the Java class path entries as a String[] array in class path order.
 String toString()
          Returns the complete Java class path as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassPath

public ClassPath()
Constructs a new ClassPath object.

Method Detail

addFile

public void addFile(File file)
             throws IOException
Adds the specified File to the system class path.

IOException

addFile

public void addFile(String fileName)
             throws IOException
Adds the specified file name to the system class path.

IOException

addURL

public void addURL(String urlName)
            throws IOException
Adds the specified URL name to the system class path.

IOException

addURL

public void addURL(URL url)
            throws IOException
Adds the specified URL to the system class path.

IOException

getAllPaths

public String[] getAllPaths()
Returns all possible Java class path directories as a String[] array in class path order.

Returns:
All possible Java class path directories as a String[] array.

getPaths

public String[] getPaths()
Returns the Java class path directories as a String[] array in class path order.

Returns:
The Java class path directories as a String[] array.

toArray

public String[] toArray()
Returns the Java class path entries as a String[] array in class path order.

Returns:
The Java class path entries as a String[] array.

toString

public String toString()
Returns the complete Java class path as a String.

Overrides:
toString in class Object
Returns:
The complete Java class path as a String.

2004.04.29



Copyright © 2004 J. Damon & Co.; All Rights Reserved WorldWide.