I have this file hierarchy:
Java Resources
src
model
Class.java
Web Content
datos.txt
I need to access from Class.java to datos.txt. I tried using C:\Users\Tomi\Documents\Eclipse\ProyectoWeb\WebContent\datos.txt
but when I use this application in another computer, it doesn't work. How can I modify that direction?
Use relative paths. For example if your application is at C:\Users\Tomi\Documents\Eclipse\ProyectoWeb then use WebContent\datos.txt as the path.
Then even if you move your application it will still look for the folder WebContent in the folder the application is running in then datos.txt within that.
For a more portable way you could embed it in the classpath an access to it via classloader, see Access to resources (images) inside jars/classpath for example or Unable to load picture from resource
Related
I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? I have tried placing the files under source package, web folder, directly under the web-application.Please help.Thanks in advance
The simplest way to get the current directory of a java application is :
System.out.println(new File(".").getAbsolutePath());
Like that you can consider the given path as the root of your application.
Cheers,
Maxime.
Read the file as a resource. Put it somewhere in the src. For instance
src/resources/myresource.txt
Then you can just do
InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");
Note: if you are using maven, then you are more accustomed to something like this
src/main/resources/myresource.txt
With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path
InputStream is = getClass().getResourceAsStream("/myresource.txt");
I'm developing a simple mail sender as Java EE application.
The project structure is shown as follows:
To properly setup email contents, I need to read the *.vm files placed inside the resource folder, that I supposed to have as path classpath:/templates/mail/*.vm (as with Spring)... But my supposition is wrong!
Which is the right path to use?
Should I have to use the META-INF folder? Is this solution more
java-ee-compliant? In that case, where have I to put the META-INF folder inside my project structure?
Update:
I packaged the project as .war, then I putted the files in:
/src/main/webapp/WEB-INF/classes/templates/mail/
Then:
org.apache.velocity.Template t = myVelocityEngine.getTemplate("classpath:/templates/mail/account_to_confirm.vm",
"UTF-8");
Nonetheless, the app returns an error at runtime:
Unable to find resource 'classpath:/templates/mail/account_to_confirm.vm'
What am I doing wrong?
Just to better understand:
Supposing that I'd like to deploy this app as jar (removing the servlet class, of course): in that case, should I have to edit the folder layout in order to still use the same path into the source code?
I think the problem is due to the prefix classpath:: where did you find that you have to use it?
You might find useful understanding how to initialize VelocityEngine reading Loading velocity template inside a jar file and how Configuring Resource Loaders in Velocity.
If you can, use Classloader.getResourceAsStream("templates/mail/*.vm"); or similar getResourceAsURL method.
If not, take a look at where files from resources are placed inside WAR. In your case, the file should be in /WEB-INF/classes/templates/mail .
I have a Java web application. Inside the WAR I have a folder containing configuration files for the application. I need to know the path of the folder in order to load the files at runtime.
I also need the solution to work in Tomcat and in WebSphere.
Thanks.
I would suggest placing the files under WEB-INF/classes and simplying loading them from the classpath, not from the filesystem. This way, the path is always the same.
You can use something like:
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("path");
this question is somewhat related to this question: StackOverflow: Howto load a resource from WEB-INF directory of a web archive
I want to use StringTemplate and want to load my templates via a StringTemplateGroup. Until know I use the method shown in the related question to get single files from the WEB-INF folder, but as I read here, it is considered bad practice to load the templates via a path as the application could be inside a WAR file AND I can not use the StringTemplateGroup because of the path.
Is there a way to achieve this that you would recommend? Can I get the entire folder as a stream-like object for the StringTemplateGroup to read or is there a decent (and not so hackish) way already implemented in StringTemplate?
I am somewhat new to Java but willing to learn :)
Thank you very much in advance.
You can add the property of context paht into JVM runtime arguments,for example, in Tomcat container,you add an argument -DcontextPath=[somewhere] into startup.sh file, and then get the path of context by System.getProperty("contextPath").The context path also can get from -Dcatalina.base in Tomcat.
There are a lot of similar questions, but, probably, mine is a little bit different:
What is the right way to load resource from inside of .jar file located in WEB-INF/lib folder (if I know the jar file name and the name of the class it resource belongs to), while Web Application is running? Should I use getServletContext().getResourceAsStream(?) for this purpose or the <name-of-known-class>.getResourseAsStream(?), and what path do I need to specify there?
So, the structure is:
/WEB-INF
/classes
/some/package/name
?.class #some Java code or Servlet that tries to read 'required-file.xml'
/lib
/<jar-with-known-name>.jar
/another/package/with/known/name
SomeKnownClass.class
required-file.xml
You should use <name-of-known-class>.getResourseAsStream(?), which loads resources using the "local" classloader. In the case of a webapp, this will use the webapp's classloader.
The getServletContext().getResourceAsStream(?) method will return webapp resources relative to the webapp root, and cannot look inside JAR files.
The javadoc for this method describes the path you need to specify, but essentially you can use paths relative to the known class, e.g.
SomeKnownClass.class.getResourceAsStream("required-file.xml");