ClassPathUtils

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 return java..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.

Download
You can download the full source code of this example here: ClassPathUtilsExample.zip

Rivu Chakraborty

Rivu Chakraborty is a Google Certified Android Developer, Sr. Tech Member of Institute of Engineers (India), he also have certifications on Scrum. He is also an author of multiple books on Kotlin, Reactive Programming, Functional Programming and Android Development, published by renowned publication houses. Having total 5+ years of experience he is presently working as a Sr. Software Engineer (Android) at Indus Net Technologies Pvt. Ltd. Rivu Chakraborty considers himself a Kotlin and Android enthusiast and a Kotlin evangelist. He has been using Kotlin since December 2015, so he has around 2 years' experience in Kotlin. As part of his mission to expand the use and knowledge of the Kotlin Language as much as possible, he created the KotlinKolkata User Group, one of the most active Kotlin user groups throughout the world and he is a Founder Organizer of KotlinKolkata. He is also an active member of GDG Kolkata and gives talks at GDG Kolkata Meetups.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button