classpath spring boot code example

Example 1: load a file from classpath spring boot

public void testResourceFile() throws IOException {
		File resource = new ClassPathResource("test.json").getFile();
		String text = new String(Files.readAllBytes(resource.toPath()));
	}

Example 2: load a file from classpath spring boot

private void testResource(Resource resource) {
		try {
			InputStream resourcee = resource.getInputStream();
			String text = null;
			try (final Reader reader = new InputStreamReader(resourcee)) {
				text = CharStreams.toString(reader);
			}
			System.out.println(text);
 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

Example 3: how to add classpath in spring boot

java -cp aapName.jar -Dloader.path=/C:/extLib/ org.springframework.boot.loader.PropertiesLauncher-

Tags:

Java Example