I am developing a spring boot application which would be used as a non-executable jar by some other user application. The spring boot application(jar) reads the xsd file present in resources folder. When we run it as a standalone application it executes fine.
Code to read the xsd file from resources folder -
File xsdValue = new ClassPathResource("xsd/" + xsdFileName + ".xsd").getFile();
But the problem is, When user application calls the jar then it tries to find the xsd in its own resources folder rather than that of jar's resources folder.
Please advise !!
Any help is much appreciated.
Resources in jar-files can not be gotten as File class, use getResourceAsStream method of ClassLoader class instead.
Related
I have created a Jersey REST project, and I am running on a Tomcat server having exported it as a WAR file. One of the operations that the project carries out is reading a CSV file from the WEB-INF directory.
However when attempting to do this, I am seeing a java.nio.file.NoSuchFileException being thrown. The read is being done from the following location:
Reader reader = Files.newBufferedReader(Paths.get("WebContent/WEB-INF/resources/mycsvfile.csv";
..which corresponds to the location in my project structure. The application is having no issue reading the file in JUnit tests, it's only when deployed that I see this issue.
Is there a specific location where resources such as files should be placed in a web based project?
Thanks for any help.
I created one java micro service using spring boot. The application requires some data from static xml files (kept together in a folder, lets name it X) in src/main/resources folder.(I am using STS IDE)
It is working fine when I run it in the IDE itself.
Now when I pack it in a jar and try running it using CLI (java -jar jarFileName.jar) it works and access the folder X from the target/classes folder.
Now my problem is, when I try to deploy this small app on Pivotal Cloud Foundry using either CLI or STS plugin, I have to give a path to the jar file so it uploads only the respective jar file and starts the container with the application. But the application cannot access folder X which contains its resources.
Though I confirmed that the jar file contains the folder X in BOOT-INF/classes but it tries to find the folder X on its own path - not inside itself.
Does anyone have some idea how can a jar file deployed on PCF can access its resources kept inside itself.
I tried using
InputStream is = this.getClass().getClassLoader().getResourceAsStream("X/abc.xml")
but it didn't work. It is unable to find the path during runtime.
why do not try Apache Abdera.
your XML is obtained from an API.
just another way, btw.
In your main class(usually Application.java) add #ImportResource annotation at class level.
Example: #ImportResource("classpath:myfile.xml")
I have implemented log4j in my web application project. Project is done using net beans,using tomcat 7.0.41. At first,I created log4j.property file and placed under web page->Web-INF->classes->log4j.properties in net beans and it asks me to locate the file in my project,so I manually located that file to implement log4j in my application. After that I changed the place of the log4j.properties file to myproject->build->web->WEB_INF->classes->log4j.properties in location of my project saved, now its working fine, it did not ask me to manually locate the property file, It takes automatically when my class files executed. Now my problem is that once I committed the project and again checkout the project on some day, property file does not appear and it again ask for property file. So where can I create the log4j property file in my project so that my team mates can utilize it when they checkout project in their system.
Normally you put log4j.properties to src/main/resources/ and it will be copied to the right place by the build process.
I never use net beans, but I think put log4j.properties under Classpath will work.
Not sure how Net Beans handels this, but i think that the "build" directory is where the "compiled" project is put to.
So i would not recommend to put any files there which should be versioned because mostly those directories are ignored for versioning ( see .gitignore files for example when using git).
Resources like property files should be within the sources and your IDE should copy them to the correct place when building the project.
Could some one help me on this problem. i have webservice , which reads data from configuration files. When i run this webservice from eclipse , i give absolute the path for these webservices of these configuration files , but when i shift the webservice in to server and run, it can not read the config file. so how can i solve this problem. is there a relative path that webservice can understand during run time.
You can put your configuration files in the root of the AAR archive or in the classes folder. Then use getResourceAsStream to read them.
ClassLoader loader = getClass().getClassLoader();
InputStream inputstream = loader.getResourceAsStream(sFilePath);
How are you deploying these configurations. If they're packed inside the .aar file I would expect them to reside in the classpath, and you could access them via Class.getResourceAsStream()
If they're deployed in the .aar file, however, they're going to be difficult to edit post deployment. In that case you may want to deploy them separately as files and put them in a location well-known to the application, and just read them as files.
I've developed a web application that worked fine in JBoss 4. Now, I need to make it work in Tomcat 6, but I'm having trouble to access some properties file. I use the following code to read read these files:
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(fileName);
if (is == null) {
throw new StartupError("Error loading " + fileName);
}
properties.load(is);
As I've said before, it works fine in JBoss 4. But when I deploy my app in tomcat, it doesn't find the file, assigning null to 'is' variable, causing the StartupError to be thrown. The file is located at WEB-INF/config directory, and the webapp is deployed as a war.
Any solution for this problem?
Thanks,
Alexandre
Put the properties files in WEB-INF/classes.
Or include them in the root of one of your webapp Jar files, although this makes it harder to edit them. This is good if you're selecting properties within a build script and don't want to edit them once deployed.
I assume that Tomcat does not add WEB-INF/config into your webapp classpath.
from http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
WebappX - A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.