Java Utililty Methods ClassLoader

List of utility methods to do ClassLoader

Description

The list of methods to do ClassLoader are organized into topic(s).

Method

StringreadFromFile(Object obj, String fileName)
read From File
return readFile(getFileFromPath(obj, fileName));
JsonNodereadSampleJson(String name)
read Sample Json
URL jsonUrl = Thread.currentThread().getContextClassLoader().getResource(name);
ObjectMapper mapper = new ObjectMapper();
try {
    return mapper.readValue(jsonUrl, JsonNode.class);
} catch (IOException e) {
    throw new IllegalStateException(e);
voidsaveProperties(Properties properties, String name)
save Properties
try {
    String configsDataDir = System.getProperty("repox.data.dir");
    String configurationFile;
    if (configDataDirAlreadyExists(configsDataDir, name))
        configurationFile = URLDecoder.decode(configsDataDir + File.separator + name, "ISO-8859-1");
    else {
        URL configurationURL = Thread.currentThread().getContextClassLoader().getResource(name);
        configurationFile = URLDecoder.decode(configurationURL.getFile(), "ISO-8859-1");
...
ListscanPackage(String path)
scan Package
List<String> clazzs = new LinkedList<>();
try {
    path = path.replaceAll("\\.", "/");
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> resources = classLoader.getResources(path);
    File[] files = new File(resources.nextElement().getFile()).listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
...
ListsearchByAttributeKey(String key)
search By Attribute Key
try {
    Enumeration<URL> resEnum = Thread.currentThread().getContextClassLoader()
            .getResources(JarFile.MANIFEST_NAME);
    List<Manifest> resultList = Lists.newLinkedList();
    while (resEnum.hasMoreElements()) {
        URL url = resEnum.nextElement();
        try (InputStream is = url.openStream()) {
            Manifest manifest = new Manifest(is);
...
voidstorePropertiesFile(String fileName, String filePath, String storeComment)
store Properties File
if (fileName == null || "".equals(fileName.trim())) {
    throw new RuntimeException("File name is null");
} else {
    fileName = fileName.trim();
    if (!fileName.toLowerCase().endsWith(".properties")) {
        fileName += ".properties";
File propertiesFile = null;
OutputStream out;
try {
    if (filePath == null || "".equals(filePath.trim())) {
        filePath = Thread.currentThread().getContextClassLoader().getResource("").toString() + fileName;
        propertiesFile = new File(new URL(filePath).toURI());
    } else {
        filePath = filePath.trim();
        if (!filePath.endsWith("/") && !filePath.endsWith("\\")) {
            filePath += "/";
        filePath += fileName;
        propertiesFile = new File(filePath);
    out = new FileOutputStream(propertiesFile);
    properties.store(out, storeComment);
    out.close();
} catch (Exception e) {
    e.printStackTrace();
Class[]toTypeArray(String[] s)
Converts an array of Class names to Class types
if (s == null)
    return null;
Class[] c = new Class[s.length];
for (int i = 0; i < s.length; i++) {
    c[i] = forName(s[i]);
return c;