Add Difference Between Class.getResource() and ClassLoader.getResource() (#12669)

* Add Difference Between Class.getResource() and ClassLoader.getResource()

* Fix - replace System.out with Logger

* Fix - move code to a different module
This commit is contained in:
apeterlic 2022-09-15 20:57:43 +02:00 committed by GitHub
parent b8a97cbebd
commit f8ea63de51
5 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.resource;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassGetResourceExample {
private static Logger logger = LoggerFactory.getLogger(ClassGetResourceExample.class);
public static void main(String[] args) {
URL resourceAbsolutePath = ClassGetResourceExample.class.getResource("/com/baeldung/resource/example.txt");
logger.info("Resource with absolute path = {}", resourceAbsolutePath);
URL resourceRelativePath = ClassGetResourceExample.class.getResource("example.txt");
logger.info("Resource with relative path = {}", resourceRelativePath);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.resource;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassLoaderGetResourceExample {
private static Logger logger = LoggerFactory.getLogger(ClassLoaderGetResourceExample.class);
public static void main(String[] args) {
URL resourceAbsolutePath = ClassLoaderGetResourceExample.class.getClassLoader()
.getResource("com/baeldung/resource/example.txt");
logger.info("Resource with absolute path = {}", resourceAbsolutePath);
URL resourceRelativePath = ClassLoaderGetResourceExample.class.getClassLoader()
.getResource("example.txt");
logger.info("Resource with relative path = {}", resourceRelativePath);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.net.URL;
class ClassGetResourceUnitTest {
@Test
void givenRelativeResourcePath_whenGetResource_thenReturnResource() {
URL resourceRelativePath = ClassGetResourceExample.class.getResource("example.txt");
Assertions.assertNotNull(resourceRelativePath);
}
@Test
void givenAbsoluteResourcePath_whenGetResource_thenReturnResource() {
URL resourceAbsolutePath = ClassGetResourceExample.class.getResource("/com/baeldung/resource/example.txt");
Assertions.assertNotNull(resourceAbsolutePath);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.net.URL;
class ClassLoaderGetResourceUnitTest {
@Test
void givenRelativeResourcePath_whenGetResource_thenReturnNull() {
URL resourceRelativePath = ClassLoaderGetResourceExample.class.getClassLoader()
.getResource("example.txt");
Assertions.assertNull(resourceRelativePath);
}
@Test
void givenAbsoluteResourcePath_whenGetResource_thenReturnResource() {
URL resourceAbsolutePath = ClassLoaderGetResourceExample.class.getClassLoader()
.getResource("com/baeldung/resource/example.txt");
Assertions.assertNotNull(resourceAbsolutePath);
}
}