Include new module

This commit is contained in:
Ssam 2022-11-20 12:17:56 +00:00
parent 3c57c4d566
commit 5e08af14a1
7 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,8 @@
## Core Java JVM Cookbooks and Examples
This module contains articles about working with the Java Virtual Machine (JVM).
### Relevant Articles:
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm-2)

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-jvm-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-jvm-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
</dependencies>
</project>

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);
}
}