Java Get Fully Qualified Name or Path of Classpath Resource using Apache Commons Lang

Tags: ClassPathUtils Apache Commons Apache Commons Lang Classpath

In this Java tutorial we learn how to populate the fully qualified name or path of classpath resources using the ClassPathUtils class of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to get fully qualified name for the resource in Java

The Apache Commons Lang library provides the method ClassPathUtils.toFullyQualifiedName() to get the fully qualified name of the classpath resource. You can learn how to use ClassPathUtils.toFullyQualifiedName() method in the following example code.

GetFullyQualifiedName1.java

import org.apache.commons.lang3.ClassPathUtils;

public class GetFullyQualifiedName1 {
    public static void main(String... args) {
        String result = ClassPathUtils.toFullyQualifiedName(ClassPathUtils.class, "configurations.properties");

        System.out.println(result);
    }
}
The output is:
org.apache.commons.lang3.configurations.properties

GetFullyQualifiedName2.java

import org.apache.commons.lang3.ClassPathUtils;

public class GetFullyQualifiedName2 {
    public static void main(String... args) {
        String result = ClassPathUtils.toFullyQualifiedName(ClassPathUtils.class.getPackage(), "configurations.properties");

        System.out.println(result);
    }
}
The output is:
org.apache.commons.lang3.configurations.properties

How to get fully qualified path for the resource in Java The Apache Commons Lang library provides the method ClassPathUtils.toFullyQualifiedPath() to get the fully qualified path for a classpath resource. You can learn how to use the ClassPathUtils.toFullyQualifiedPath() method in the code example below.

GetFullyQualifiedPath1.java

import org.apache.commons.lang3.ClassPathUtils;

public class GetFullyQualifiedPath1 {
    public static void main(String... args) {
        String result = ClassPathUtils.toFullyQualifiedPath(ClassPathUtils.class, "configurations.properties");

        System.out.println(result);
    }
}
The output is:
org/apache/commons/lang3/configurations.properties

GetFullyQualifiedPath2.java

import org.apache.commons.lang3.ClassPathUtils;

public class GetFullyQualifiedPath2 {
    public static void main(String... args) {
        String result = ClassPathUtils.toFullyQualifiedPath(ClassPathUtils.class.getPackage(), "configurations.properties");

        System.out.println(result);
    }
}
The output is:
org/apache/commons/lang3/configurations.properties

Happy Coding 😊