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");
Related
what I want to ask seems so simple and crazy but since I am so beginner I dare to ask you guys.
I want to give relative address to read a file in eclipse java. my java file is in common package and json file is in resources package in the same project. but I do not know how to provide relative address to that.
BufferedReader in = new BufferedReader(new FileReader("/?/file.json"));
so I have a project:
> src/main/java
>com.project.cc.restful.common
>com.project.cc.restful.resources
any help?
thanks!
What you need is a path that is relative to your working directory. The working directory is a configurable parameter. In eclipse the default is usually the root folder of the project (not the source code folder!). It can be configured in the "Run Configurations.." menu.
To be sure, run your application once with System.out.println(System.getProperty("user.dir")) to see the absolute path to your working directory. Once you have that, use ../../Resources (or something similar) to get to the resources directory using a relative path.
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 .
My GWT project runs nicely in developement mode but when I put it on server it can't find an XML file.
My file is in src/main/webapp and when I do mvn install it shows up in target/<projectname>-1.0-SNAPSHOT
I try to access the file like this:
FileInputStream inputStream = new FileInputStream("testobj.xml");
and it throws
java.io.FileNotFoundException: testobj.xml (The system cannot find the file specified)
really puzzled by it .. haven't found any useful links on this either.
FileInputStream inputStream = new FileInputStream("testobj.xml");
To make that a valid path you have to place the xml in same folder where your class file is there.
And the good practice is that put the file in WEB-INF folder and access the path like
getSevletContext.getRealPath("/WEB-INF/resources/testobj.xml");
You might placed the file in src and it is taking from system path. Once you compile the project, your java files converts to class files and places in WEB-INF/classes folder, where the context has been changed.
To maintain the consistency for both in development mode and live environment access files from WEB-INF folder with real path.
It can be seen in development because testobj.xml is able to be found on the path in development. After your project is packaged and built it needs to be in your WEB-INF folder in the war. It is generally good practices to put your resources in src/main/resources as well, not the root folder.
Whatever you are using for your build will need to copy your resources to WEB-INF when creating a war. If you are using maven see this thread for how to accomplish this: Maven: how to get a war package with resources copied in WEB-INF?
FileInputStream inputStream = new FileInputStream("testobj.xml");
This line tries to access "testobj.xml" in the process's current directory. When you run this within a web app, it'll look for the file within the application server process's current directory. This directory could be anything, and it's unlikely that the file will be there.
The normal way to read resources packaged with the web app is to use the web app's ClassLoader:
InputStream is = getClass().getClassLoader().getResourceAsStream("resources/testobj.xml");
This will automatically search the web application's deployment files for the named file. See this question for more discussion.
I'm trying to read from a text file in Netbeans. In the top level of my project directory I have foo.txt. Then in my code I have:
File file = new File("foo.txt");
It throws a FileNotFoundException, however. It's a Java web application using Spring and Tomcat, but I'm not sure if those details matter since I'm running the whole thing inside Netbeans. Basically, I just want to know where I need to put the file so Netbeans will read it.
Update - good call guys, it's looking in Tomcat's bin directory. Now this may be a stupid question but, how would I go about getting it to look in my top level project directory? I feel like dropping text files into tomcat's bin would be innapropriate.
You can try printing the absolute path of the File object to see where it is looking on the filesystem.
System.out.println(file.getAbsolutePath());
I would use the following to figure out where to put the file:
System.out.println(System.getProperty("user.dir"));
To directly answer your question, If you're running an application on Tomcat, files will be opened from the current working directory. That will likely be the bin/ folder in your tomcat directory.
You can find out for sure where your program is looking by examining the result of file.getAbsolutePath().
However, for web applications, I would suggest putting files you need to read in your classpath so you don't have to depend on a certain file structure when you deploy your web application.
try System.getProperty("user.dir") to get current working directory
I'm working with a project that is setup using the standard Maven directory structure so I have a folder called "resources" and within this I have made a folder called "fonts" and then put a file in it. I need to pass in the full String file path (of a file that is located, within my project structure, at resources/fonts/somefont.ttf) to an object I am using, from a 3rd party library, as below, I have searched on this for a while but have become a bit confused as to the proper way to do this. I have tried as below but it isn't able to find it. I looked at using ResourceBundle but that seemed to involve making an actual File object when I just need the path to pass into a method like the one below (don't have the actual method call in front of me so just giving an example from my memory):
FontFactory.somemethod("resources/fonts/somefont.ttf");
I had thought there was a way, with a project with standard Maven directory structure to get a file from the resource folder without having to use the full relative path from the class / package. Any advice on this is greatly appreciated.
I don't want to use a hard-coded path since different developers who work on the project have different setups and I want to include this as part of the project so that they get it directly when they checkout the project source.
This is for a web application (Struts 1.3 app) and when I look into the exploded WAR file (which I am running the project off of through Tomcat), the file is at:
<Exploded war dir>/resources/fonts/somefont.ttf
Code:
import java.io.File;
import org.springframework.core.io.*;
public String getFontFilePath(String classpathRelativePath) {
Resource rsrc = new ClassPathResource(classpathRelativePath);
return rsrc.getFile().getAbsolutePath();
}
In your case, classpathRelativePath would be something like "/resources/fonts/somefont.ttf".
You can use the below mentioned to get the path of the file:
String fileName = "/filename.extension"; //use forward slash to recognize your file
String path = this.getClass().getResource(fileName).toString();
use/pass the path to your methods.
If your resources directory is in the root of your war, that means resources/fonts/somefont.ttf would be a "virtual path" where that file is available. You can get the "real path"--the absolute file system path--from the ServletContext. Note (in the docs) that this only works if the WAR is exploded. If your container runs the app from the war file without expanding it, this method won't work.
You can look up the answer to the question on similar lines which I had
Loading XML Files during Maven Test run
The answer given by BobG should work. Though you need to keep in mind that path for the resource file is relative to path of the current class. Both resources and java source files are in classpath