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:
		
							parent
							
								
									b8a97cbebd
								
							
						
					
					
						commit
						f8ea63de51
					
				| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1 @@ | ||||
| Baeldung | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user