EAR classloader doesn't see ApplicationInsights.xml located in jarIncluding all the jars in a directory within the Java classpathHow to get the path of a running JAR file?How can I create an executable JAR with dependencies using Maven?How to build jars from IntelliJ properly?.war vs .ear fileAndroid SDK installation doesn't find JDKHow to add local jar files to a Maven project?Difference between jar and war in JavaCan't execute jar- file: “no main manifest attribute”Android Studio: Add jar as library?

Wiring IKEA light fixture into old fixture

Is there a way to shorten this while condition?

Do you have to hide in order for other creatures to not know your location inside a fog cloud?

Why does the salt in the oceans not sink to the bottom?

Why do people say "I am broke" instead of "I am broken"?

What does the following chess proverb mean: "Chess is a sea where a gnat may drink from and an elephant may bathe in."

How to plot a crossection of a ParametricPlot3D?

How can I show that the speed of light in vacuum is the same in all reference frames?

Planck's blackbody curve with "rainbow" in Tikz

What does a black-and-white Puerto Rican flag signify?

Can a creature sustain itself by eating its own severed body parts?

Magic is the twist

Recruiter wants to represent me for a role that I've already applied to

Does switching on an old games console without a cartridge damage it?

If hash functions append the length, why does length extension attack work?

"It is what it is" in French

What's the 1 inch size square knob sticking out of wall?

RC differentiator giving a higher output amplitude than input amplitude

What kind of curve (or model) should I fit to my percentage data?

Pass USB 3.0 connection through D-SUB connector

Higher calorie ice cream

Why did NASA use Imperial units?

Are gangsters hired to attack people at a train station classified as a terrorist attack?

Learning Ramban Al HaTorah



EAR classloader doesn't see ApplicationInsights.xml located in jar


Including all the jars in a directory within the Java classpathHow to get the path of a running JAR file?How can I create an executable JAR with dependencies using Maven?How to build jars from IntelliJ properly?.war vs .ear fileAndroid SDK installation doesn't find JDKHow to add local jar files to a Maven project?Difference between jar and war in JavaCan't execute jar- file: “no main manifest attribute”Android Studio: Add jar as library?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have the following problem: I try to deploy the Azure telemetry configuration in jar file that is located in the war file inside main ear. Unfortunately, during server startup some errors occur as ApplicationInsights.xml cannot be found. I debugged it and I found out that it cannot be read by com.microsoft.applicationinsights.internal.config.ConfigurationFileLocator.getConfigurationFile().



There is a structure of my module:



azure
|_ src
|_ main
|_ java
| |_ mypackage
| |_ MicrometerRegistryConfigurationListener.java
|_ resources
|_ ApplicationInsights.xml


There is MicrometerRegistryConfigurationListener.java:



@WebListener
public class MicrometerRegistryConfigurationListener implements ServletContextListener
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
MeterRegistry azureMeterRegistry = new AzureMonitorMeterRegistry(new AzureMonitorConfig()
@Override
public String get(String key)
return null;


@Override
public Duration step()
return Duration.ofSeconds(5);

, Clock.SYSTEM);

new JvmThreadMetrics().bindTo(azureMeterRegistry);
new JvmMemoryMetrics().bindTo(azureMeterRegistry);
new JvmGcMetrics().bindTo(azureMeterRegistry);

servletContextEvent.getServletContext().setAttribute("AzureMonitorMeterRegistry", azureMeterRegistry);


@Override
public void contextDestroyed(ServletContextEvent sce)





And there is a structure of ear:



ear
|_ war
|_ WEB-INF/lib
|_ azure.jar
|_ ApplicationInsights.xml


Of course, azure.jar contains also classes, i.a. MicrometerRegistryConfigurationListener.



What is going on? Why ApplicationInsights.xml is not visible?










share|improve this question






















  • It needs to be read as an InputStream as a CLASSPATH resource. Either that's not being done properly OR the classpath isn't set properly.

    – duffymo
    Mar 26 at 14:24











  • How to set the classpath?

    – wojtek1902
    Mar 26 at 21:23











  • I think it would be in META-INF

    – duffymo
    Mar 26 at 21:24

















0















I have the following problem: I try to deploy the Azure telemetry configuration in jar file that is located in the war file inside main ear. Unfortunately, during server startup some errors occur as ApplicationInsights.xml cannot be found. I debugged it and I found out that it cannot be read by com.microsoft.applicationinsights.internal.config.ConfigurationFileLocator.getConfigurationFile().



There is a structure of my module:



azure
|_ src
|_ main
|_ java
| |_ mypackage
| |_ MicrometerRegistryConfigurationListener.java
|_ resources
|_ ApplicationInsights.xml


There is MicrometerRegistryConfigurationListener.java:



@WebListener
public class MicrometerRegistryConfigurationListener implements ServletContextListener
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
MeterRegistry azureMeterRegistry = new AzureMonitorMeterRegistry(new AzureMonitorConfig()
@Override
public String get(String key)
return null;


@Override
public Duration step()
return Duration.ofSeconds(5);

, Clock.SYSTEM);

new JvmThreadMetrics().bindTo(azureMeterRegistry);
new JvmMemoryMetrics().bindTo(azureMeterRegistry);
new JvmGcMetrics().bindTo(azureMeterRegistry);

servletContextEvent.getServletContext().setAttribute("AzureMonitorMeterRegistry", azureMeterRegistry);


@Override
public void contextDestroyed(ServletContextEvent sce)





And there is a structure of ear:



ear
|_ war
|_ WEB-INF/lib
|_ azure.jar
|_ ApplicationInsights.xml


Of course, azure.jar contains also classes, i.a. MicrometerRegistryConfigurationListener.



What is going on? Why ApplicationInsights.xml is not visible?










share|improve this question






















  • It needs to be read as an InputStream as a CLASSPATH resource. Either that's not being done properly OR the classpath isn't set properly.

    – duffymo
    Mar 26 at 14:24











  • How to set the classpath?

    – wojtek1902
    Mar 26 at 21:23











  • I think it would be in META-INF

    – duffymo
    Mar 26 at 21:24













0












0








0








I have the following problem: I try to deploy the Azure telemetry configuration in jar file that is located in the war file inside main ear. Unfortunately, during server startup some errors occur as ApplicationInsights.xml cannot be found. I debugged it and I found out that it cannot be read by com.microsoft.applicationinsights.internal.config.ConfigurationFileLocator.getConfigurationFile().



There is a structure of my module:



azure
|_ src
|_ main
|_ java
| |_ mypackage
| |_ MicrometerRegistryConfigurationListener.java
|_ resources
|_ ApplicationInsights.xml


There is MicrometerRegistryConfigurationListener.java:



@WebListener
public class MicrometerRegistryConfigurationListener implements ServletContextListener
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
MeterRegistry azureMeterRegistry = new AzureMonitorMeterRegistry(new AzureMonitorConfig()
@Override
public String get(String key)
return null;


@Override
public Duration step()
return Duration.ofSeconds(5);

, Clock.SYSTEM);

new JvmThreadMetrics().bindTo(azureMeterRegistry);
new JvmMemoryMetrics().bindTo(azureMeterRegistry);
new JvmGcMetrics().bindTo(azureMeterRegistry);

servletContextEvent.getServletContext().setAttribute("AzureMonitorMeterRegistry", azureMeterRegistry);


@Override
public void contextDestroyed(ServletContextEvent sce)





And there is a structure of ear:



ear
|_ war
|_ WEB-INF/lib
|_ azure.jar
|_ ApplicationInsights.xml


Of course, azure.jar contains also classes, i.a. MicrometerRegistryConfigurationListener.



What is going on? Why ApplicationInsights.xml is not visible?










share|improve this question














I have the following problem: I try to deploy the Azure telemetry configuration in jar file that is located in the war file inside main ear. Unfortunately, during server startup some errors occur as ApplicationInsights.xml cannot be found. I debugged it and I found out that it cannot be read by com.microsoft.applicationinsights.internal.config.ConfigurationFileLocator.getConfigurationFile().



There is a structure of my module:



azure
|_ src
|_ main
|_ java
| |_ mypackage
| |_ MicrometerRegistryConfigurationListener.java
|_ resources
|_ ApplicationInsights.xml


There is MicrometerRegistryConfigurationListener.java:



@WebListener
public class MicrometerRegistryConfigurationListener implements ServletContextListener
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
MeterRegistry azureMeterRegistry = new AzureMonitorMeterRegistry(new AzureMonitorConfig()
@Override
public String get(String key)
return null;


@Override
public Duration step()
return Duration.ofSeconds(5);

, Clock.SYSTEM);

new JvmThreadMetrics().bindTo(azureMeterRegistry);
new JvmMemoryMetrics().bindTo(azureMeterRegistry);
new JvmGcMetrics().bindTo(azureMeterRegistry);

servletContextEvent.getServletContext().setAttribute("AzureMonitorMeterRegistry", azureMeterRegistry);


@Override
public void contextDestroyed(ServletContextEvent sce)





And there is a structure of ear:



ear
|_ war
|_ WEB-INF/lib
|_ azure.jar
|_ ApplicationInsights.xml


Of course, azure.jar contains also classes, i.a. MicrometerRegistryConfigurationListener.



What is going on? Why ApplicationInsights.xml is not visible?







java azure gradle jar ear






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 14:07









wojtek1902wojtek1902

11910 bronze badges




11910 bronze badges












  • It needs to be read as an InputStream as a CLASSPATH resource. Either that's not being done properly OR the classpath isn't set properly.

    – duffymo
    Mar 26 at 14:24











  • How to set the classpath?

    – wojtek1902
    Mar 26 at 21:23











  • I think it would be in META-INF

    – duffymo
    Mar 26 at 21:24

















  • It needs to be read as an InputStream as a CLASSPATH resource. Either that's not being done properly OR the classpath isn't set properly.

    – duffymo
    Mar 26 at 14:24











  • How to set the classpath?

    – wojtek1902
    Mar 26 at 21:23











  • I think it would be in META-INF

    – duffymo
    Mar 26 at 21:24
















It needs to be read as an InputStream as a CLASSPATH resource. Either that's not being done properly OR the classpath isn't set properly.

– duffymo
Mar 26 at 14:24





It needs to be read as an InputStream as a CLASSPATH resource. Either that's not being done properly OR the classpath isn't set properly.

– duffymo
Mar 26 at 14:24













How to set the classpath?

– wojtek1902
Mar 26 at 21:23





How to set the classpath?

– wojtek1902
Mar 26 at 21:23













I think it would be in META-INF

– duffymo
Mar 26 at 21:24





I think it would be in META-INF

– duffymo
Mar 26 at 21:24












1 Answer
1






active

oldest

votes


















0














Cause jar is a file, it's not a folder. The loader tries to access a file system path, but it can not access a path in your JAR. So you could use the resource.getInputStream() method to retrieve it as a InputStream. You could get details from this doc:Java: Load file from classpath in Spring Boot.



Example code:



public class EnvResolver 
private ResourceLoader resourceLoader;
public EnvResolver()
resourceLoader = new DefaultResourceLoader();

public List GetEnvConfig() throws IOException
Yaml yaml = new Yaml();
Resource resource = resourceLoader.getResource("classpath:test-environment.yml");
System.out.println("FileName: " + resource.getFilename());
InputStream inputStream = resource.getInputStream();
Map<String, Map<String, String>> result = yaml.load(inputStream);
List<TestEnvVo> envList = new ArrayList<>();
for (String key : result.keySet())
TestEnvVo testEnvVo = new TestEnvVo();
testEnvVo.setEnvName(result.get(key).get("name"));
testEnvVo.setDomain(result.get(key).get("domain"));
envList.add(testEnvVo);

inputStream.close();
return envList;




After searching, I found another way to configure it, in your POM file add the resources configuration. You could have a try.



<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>


Hope this could help you.



UPDATE:



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Hello

public static void main(String[] args) throws IOException
InputStream resourceInputStream = null;
URL resourceURL = Hello.class.getClassLoader().getResource("resources/hello.json");
if(resourceURL == null)
System.out.println("Get the InputStream of file in IDE");
resourceInputStream = new FileInputStream(Hello.class.getClassLoader().getResource("").getPath()+"../../src/main/resources/hello.json");
else
System.out.println("Get the InputStream of file from runnable jar");
resourceInputStream = Hello.class.getClassLoader().getResourceAsStream("resources/hello.json");

System.out.println();
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(resourceInputStream));
String line = null;
while((line = br.readLine()) != null)
builder.append(line+"n");

br.close();
System.out.println(builder.toString());







share|improve this answer

























  • But I use gradle. How would it look like in gradle?

    – wojtek1902
    Mar 27 at 8:05











  • @wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

    – George Chen
    Mar 27 at 9:34












  • But I use gradle, I don't have POM file. And the file is loaded in the following way:

    – wojtek1902
    Mar 27 at 10:34











  • @wojtek1902,you could add something in build.gradle to generate the pom file:

    – George Chen
    Mar 27 at 10:57











  • apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

    – George Chen
    Mar 27 at 10:57










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55359194%2fear-classloader-doesnt-see-applicationinsights-xml-located-in-jar%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Cause jar is a file, it's not a folder. The loader tries to access a file system path, but it can not access a path in your JAR. So you could use the resource.getInputStream() method to retrieve it as a InputStream. You could get details from this doc:Java: Load file from classpath in Spring Boot.



Example code:



public class EnvResolver 
private ResourceLoader resourceLoader;
public EnvResolver()
resourceLoader = new DefaultResourceLoader();

public List GetEnvConfig() throws IOException
Yaml yaml = new Yaml();
Resource resource = resourceLoader.getResource("classpath:test-environment.yml");
System.out.println("FileName: " + resource.getFilename());
InputStream inputStream = resource.getInputStream();
Map<String, Map<String, String>> result = yaml.load(inputStream);
List<TestEnvVo> envList = new ArrayList<>();
for (String key : result.keySet())
TestEnvVo testEnvVo = new TestEnvVo();
testEnvVo.setEnvName(result.get(key).get("name"));
testEnvVo.setDomain(result.get(key).get("domain"));
envList.add(testEnvVo);

inputStream.close();
return envList;




After searching, I found another way to configure it, in your POM file add the resources configuration. You could have a try.



<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>


Hope this could help you.



UPDATE:



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Hello

public static void main(String[] args) throws IOException
InputStream resourceInputStream = null;
URL resourceURL = Hello.class.getClassLoader().getResource("resources/hello.json");
if(resourceURL == null)
System.out.println("Get the InputStream of file in IDE");
resourceInputStream = new FileInputStream(Hello.class.getClassLoader().getResource("").getPath()+"../../src/main/resources/hello.json");
else
System.out.println("Get the InputStream of file from runnable jar");
resourceInputStream = Hello.class.getClassLoader().getResourceAsStream("resources/hello.json");

System.out.println();
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(resourceInputStream));
String line = null;
while((line = br.readLine()) != null)
builder.append(line+"n");

br.close();
System.out.println(builder.toString());







share|improve this answer

























  • But I use gradle. How would it look like in gradle?

    – wojtek1902
    Mar 27 at 8:05











  • @wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

    – George Chen
    Mar 27 at 9:34












  • But I use gradle, I don't have POM file. And the file is loaded in the following way:

    – wojtek1902
    Mar 27 at 10:34











  • @wojtek1902,you could add something in build.gradle to generate the pom file:

    – George Chen
    Mar 27 at 10:57











  • apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

    – George Chen
    Mar 27 at 10:57















0














Cause jar is a file, it's not a folder. The loader tries to access a file system path, but it can not access a path in your JAR. So you could use the resource.getInputStream() method to retrieve it as a InputStream. You could get details from this doc:Java: Load file from classpath in Spring Boot.



Example code:



public class EnvResolver 
private ResourceLoader resourceLoader;
public EnvResolver()
resourceLoader = new DefaultResourceLoader();

public List GetEnvConfig() throws IOException
Yaml yaml = new Yaml();
Resource resource = resourceLoader.getResource("classpath:test-environment.yml");
System.out.println("FileName: " + resource.getFilename());
InputStream inputStream = resource.getInputStream();
Map<String, Map<String, String>> result = yaml.load(inputStream);
List<TestEnvVo> envList = new ArrayList<>();
for (String key : result.keySet())
TestEnvVo testEnvVo = new TestEnvVo();
testEnvVo.setEnvName(result.get(key).get("name"));
testEnvVo.setDomain(result.get(key).get("domain"));
envList.add(testEnvVo);

inputStream.close();
return envList;




After searching, I found another way to configure it, in your POM file add the resources configuration. You could have a try.



<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>


Hope this could help you.



UPDATE:



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Hello

public static void main(String[] args) throws IOException
InputStream resourceInputStream = null;
URL resourceURL = Hello.class.getClassLoader().getResource("resources/hello.json");
if(resourceURL == null)
System.out.println("Get the InputStream of file in IDE");
resourceInputStream = new FileInputStream(Hello.class.getClassLoader().getResource("").getPath()+"../../src/main/resources/hello.json");
else
System.out.println("Get the InputStream of file from runnable jar");
resourceInputStream = Hello.class.getClassLoader().getResourceAsStream("resources/hello.json");

System.out.println();
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(resourceInputStream));
String line = null;
while((line = br.readLine()) != null)
builder.append(line+"n");

br.close();
System.out.println(builder.toString());







share|improve this answer

























  • But I use gradle. How would it look like in gradle?

    – wojtek1902
    Mar 27 at 8:05











  • @wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

    – George Chen
    Mar 27 at 9:34












  • But I use gradle, I don't have POM file. And the file is loaded in the following way:

    – wojtek1902
    Mar 27 at 10:34











  • @wojtek1902,you could add something in build.gradle to generate the pom file:

    – George Chen
    Mar 27 at 10:57











  • apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

    – George Chen
    Mar 27 at 10:57













0












0








0







Cause jar is a file, it's not a folder. The loader tries to access a file system path, but it can not access a path in your JAR. So you could use the resource.getInputStream() method to retrieve it as a InputStream. You could get details from this doc:Java: Load file from classpath in Spring Boot.



Example code:



public class EnvResolver 
private ResourceLoader resourceLoader;
public EnvResolver()
resourceLoader = new DefaultResourceLoader();

public List GetEnvConfig() throws IOException
Yaml yaml = new Yaml();
Resource resource = resourceLoader.getResource("classpath:test-environment.yml");
System.out.println("FileName: " + resource.getFilename());
InputStream inputStream = resource.getInputStream();
Map<String, Map<String, String>> result = yaml.load(inputStream);
List<TestEnvVo> envList = new ArrayList<>();
for (String key : result.keySet())
TestEnvVo testEnvVo = new TestEnvVo();
testEnvVo.setEnvName(result.get(key).get("name"));
testEnvVo.setDomain(result.get(key).get("domain"));
envList.add(testEnvVo);

inputStream.close();
return envList;




After searching, I found another way to configure it, in your POM file add the resources configuration. You could have a try.



<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>


Hope this could help you.



UPDATE:



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Hello

public static void main(String[] args) throws IOException
InputStream resourceInputStream = null;
URL resourceURL = Hello.class.getClassLoader().getResource("resources/hello.json");
if(resourceURL == null)
System.out.println("Get the InputStream of file in IDE");
resourceInputStream = new FileInputStream(Hello.class.getClassLoader().getResource("").getPath()+"../../src/main/resources/hello.json");
else
System.out.println("Get the InputStream of file from runnable jar");
resourceInputStream = Hello.class.getClassLoader().getResourceAsStream("resources/hello.json");

System.out.println();
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(resourceInputStream));
String line = null;
while((line = br.readLine()) != null)
builder.append(line+"n");

br.close();
System.out.println(builder.toString());







share|improve this answer















Cause jar is a file, it's not a folder. The loader tries to access a file system path, but it can not access a path in your JAR. So you could use the resource.getInputStream() method to retrieve it as a InputStream. You could get details from this doc:Java: Load file from classpath in Spring Boot.



Example code:



public class EnvResolver 
private ResourceLoader resourceLoader;
public EnvResolver()
resourceLoader = new DefaultResourceLoader();

public List GetEnvConfig() throws IOException
Yaml yaml = new Yaml();
Resource resource = resourceLoader.getResource("classpath:test-environment.yml");
System.out.println("FileName: " + resource.getFilename());
InputStream inputStream = resource.getInputStream();
Map<String, Map<String, String>> result = yaml.load(inputStream);
List<TestEnvVo> envList = new ArrayList<>();
for (String key : result.keySet())
TestEnvVo testEnvVo = new TestEnvVo();
testEnvVo.setEnvName(result.get(key).get("name"));
testEnvVo.setDomain(result.get(key).get("domain"));
envList.add(testEnvVo);

inputStream.close();
return envList;




After searching, I found another way to configure it, in your POM file add the resources configuration. You could have a try.



<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>


Hope this could help you.



UPDATE:



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Hello

public static void main(String[] args) throws IOException
InputStream resourceInputStream = null;
URL resourceURL = Hello.class.getClassLoader().getResource("resources/hello.json");
if(resourceURL == null)
System.out.println("Get the InputStream of file in IDE");
resourceInputStream = new FileInputStream(Hello.class.getClassLoader().getResource("").getPath()+"../../src/main/resources/hello.json");
else
System.out.println("Get the InputStream of file from runnable jar");
resourceInputStream = Hello.class.getClassLoader().getResourceAsStream("resources/hello.json");

System.out.println();
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(resourceInputStream));
String line = null;
while((line = br.readLine()) != null)
builder.append(line+"n");

br.close();
System.out.println(builder.toString());








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 10:59

























answered Mar 27 at 6:34









George ChenGeorge Chen

3,7341 gold badge1 silver badge12 bronze badges




3,7341 gold badge1 silver badge12 bronze badges












  • But I use gradle. How would it look like in gradle?

    – wojtek1902
    Mar 27 at 8:05











  • @wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

    – George Chen
    Mar 27 at 9:34












  • But I use gradle, I don't have POM file. And the file is loaded in the following way:

    – wojtek1902
    Mar 27 at 10:34











  • @wojtek1902,you could add something in build.gradle to generate the pom file:

    – George Chen
    Mar 27 at 10:57











  • apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

    – George Chen
    Mar 27 at 10:57

















  • But I use gradle. How would it look like in gradle?

    – wojtek1902
    Mar 27 at 8:05











  • @wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

    – George Chen
    Mar 27 at 9:34












  • But I use gradle, I don't have POM file. And the file is loaded in the following way:

    – wojtek1902
    Mar 27 at 10:34











  • @wojtek1902,you could add something in build.gradle to generate the pom file:

    – George Chen
    Mar 27 at 10:57











  • apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

    – George Chen
    Mar 27 at 10:57
















But I use gradle. How would it look like in gradle?

– wojtek1902
Mar 27 at 8:05





But I use gradle. How would it look like in gradle?

– wojtek1902
Mar 27 at 8:05













@wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

– George Chen
Mar 27 at 9:34






@wojtek1902, you could use classLoader getResource method to read file, I update the example code. And maybe you could try to edit the POM file the way I posted.

– George Chen
Mar 27 at 9:34














But I use gradle, I don't have POM file. And the file is loaded in the following way:

– wojtek1902
Mar 27 at 10:34





But I use gradle, I don't have POM file. And the file is loaded in the following way:

– wojtek1902
Mar 27 at 10:34













@wojtek1902,you could add something in build.gradle to generate the pom file:

– George Chen
Mar 27 at 10:57





@wojtek1902,you could add something in build.gradle to generate the pom file:

– George Chen
Mar 27 at 10:57













apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

– George Chen
Mar 27 at 10:57





apply plugin: 'maven' task writeNewPom << pom project inceptionYear '2018' licenses license name 'The Apache Software License, Version 2.0' url 'apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' .writeTo("pom.xml")

– George Chen
Mar 27 at 10:57






Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55359194%2fear-classloader-doesnt-see-applicationinsights-xml-located-in-jar%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해