org.apache.commons.lang3.ClassPathUtils Example
In this example we are going to elaborate the use of the ClassPathUtils
class in the package: org.apache.commons.lang3
or previouslyorg.apache.commons.lang
, as the package name says, it is a member of the Apache Commons Lang, and deals with the classpaths. As like other classes of the Apache Commons This class also provides us with some really helpful methods. The methods of this class as other classes of Apache Commons are wrappers for classpath Manipulation so the code which implemented those methods becomes significantly smaller, cleaner and understandable comparing to other programs where those functionalities are written manually.
1. The ClassPathUtils class overview, Fields & Methods.
The ClassPathUtils
class in the org.apache.commons.lang3
package is one of the smallest class in the Apache Commons Lang3. It contains just two method with two overloads for each. This class contains no methods or fields. Below you can see the method details and usage.
1.1 The ClassPathUtils Method Summary & Usage
static String toFullyQualifiedName(Class context, String resourceName)
: This method returns the fully qualified name for the resource with name resourceName relative to the given context. This method also has a overload in which we can pass the package as the context. Null inputs are not allowed.Note that this method does not check whether the resource actually exists, it only constructs the name, i.e. if you pass
String.class
as context and"String.abc"
as the resourceName then it will returnjava..lang.String.abc
without checking.Code
System.out.println(ClassPathUtils.toFullyQualifiedName(Math.class,"Math.noClassExists" )); System.out.println(ClassPathUtils.toFullyQualifiedName(Character.class, "Character.UnicodeBlock")); System.out.println(ClassPathUtils.toFullyQualifiedName(Reader.class.getPackage(), "FileReader"));
Output
java.lang.Math.noClassExists java.lang.Character.UnicodeBlock java.io.FileReader
static String toFullyQualifiedName(Class context, String resourceName)
: This method is also same as previous method except that it returns the fully qualified path for the resource with name resourceName relative to the given context. This method also has a overload in which we can pass the package as the context. Null inputs are not allowed.Code
System.out.println(ClassPathUtils.toFullyQualifiedPath(Math.class,"Math.noClassExists" )); System.out.println(ClassPathUtils.toFullyQualifiedPath(Character.class, "Character.UnicodeBlock")); System.out.println(ClassPathUtils.toFullyQualifiedPath(Reader.class.getPackage(), "FileReader"));
Output
java/lang/Math.noClassExists java/lang/Character.UnicodeBlock java/io/FileReader
2. ClassPathUtils Example
The complete ClassPathUtilsExample.java
package com.javacodegeeks.example.classpathutilsexample; import org.apache.commons.lang3.*; import java.io.*; import java.util.*; public class ClassPathUtilsExample { public static void main(String args[]) { System.out.println(ClassPathUtils.toFullyQualifiedName(Math.class,"Math.noClassExists" )); System.out.println(ClassPathUtils.toFullyQualifiedName(Character.class, "Character.UnicodeBlock")); System.out.println(ClassPathUtils.toFullyQualifiedName(Reader.class.getPackage(), "FileReader")); System.out.println(ClassPathUtils.toFullyQualifiedPath(Math.class,"Math.noClassExists" )); System.out.println(ClassPathUtils.toFullyQualifiedPath(Character.class, "Character.UnicodeBlock")); System.out.println(ClassPathUtils.toFullyQualifiedPath(Reader.class.getPackage(), "FileReader")); } }
Output
java.lang.Math.noClassExists java.lang.Character.UnicodeBlock java.io.FileReader java/lang/Math.noClassExists java/lang/Character.UnicodeBlock java/io/FileReader
Download The Source Code
This was an example for ClassPathUtils
in Apache Commons lang3.
You can download the full source code of this example here: ClassPathUtilsExample.zip