I've a service class in src.service.ABCservice
In ABCService class, I need to write in a xml file.
The xml file is in resource folder, which is at same level of src folder
When i run it as a single java class though main method, I can write in the xml using path ./resource/aaa.xml but when I run it on my tomcat server, it fails to read the file.
ERROR : java.io.FileNotFoundException: .\resources\LocationOfOperation.xml (The system cannot find the path specified)
Please tell me how to reach the xml file and write in it.
When you run as single class, you can read project's file path, but when you package and deploy as web project you miss project path reference.
You must create a parameter (context param, file properties, ...) to point to a path that existing into web part, thus you can use the files contained in that path to your porpouses.
Related
I work on a Java console application. There is a property in my application.properties file, which contains another file name as a value of a property, like
my.file.location=file:myDir/myFileName
In the code I try to get the file like this:
#Value("${my.file.location}")
private File myfileLocation;
If I start the application from the directory, which contains jar file, the file is resolved, but when I run my application from a different location, the file location is not valid.
I can't have this file on classpath, it must be external to the jar file.
How can I make the file path to be relative to my jar file and not to the current working directory?
I believe this has nothing to do with Spring right? You just want to load configuration file, that is inside your application, unpacked, so the user can modify it, ok?
First, you may try to always setup the working directory, which I believe is more "standard" solution. In windows you can make a link, that specifies the Start in section and contains the path to your jar file (or bat or cmd, whatever).
If you insist on using the jar path, you could use How to get the path of a running JAR file solution. Note, that the jar must be loaded from filesystem:
URI path = MySpringBean.class.getProtectionDomain().getCodeSource().getLocation().toURI();
File myfileLocation = new File(new File(path).getParent(), "/myDir/jdbc.properties");
From a java application i made, I build the corresponding jar file.
I copy all the resources in the jar file.
For example, if in src/main/resources there exists the following resource /folder/my-file, then it's copied in the jar with the same path.
But if i execute the jar, the loading of the resources fails.
Specifically, it throws an IOException like this
java.io.IOException: Unable to resolve
"file:/my-jar.jar!/folder/my-file" as either class path, filename or
URL
How should I load the resources?
If I run the java app in eclipse, all works fine, even the resources are loaded correctly.
EDIT:
I'm getting the path of the resource via:
MyClass.class.getResource("/folder/my-file").getPath();
The loading of the resource is made by an external library, what i have to d is just specify the path.
In that case, I would try:
YourClass.class.getClassLoader().getResourceAsStream("folder/my-file");
Which would return an InputStream to your resource independently of your execution environment .
if you have an structure like this:
src/main/Begin.java
src/main/folder/icon.png
Try this in the Begin.java:
Begin.getClass().getResource("/main/folder/icon.png");
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
I am trying to access a data file from a public class, both of which are located within a JAR file. However, when I execute the jar on a Hadoop cluster, the system throws a FileNotFoundException. The bottom line is: is it possible to access resources within a Jar when running an application on a cluster, or does the resource need to be copied to the HDFS individually, and for either of the above, how would you go about implementing it?
Thanks!
Yes, if the JAR is in the CLASSPATH you can call getResourceAsStream() using a class loader or servlet context to get a reference to an InputStream for that file.
You will NOT have access to the file path. You give a path relative to the CLASSPATH and the classloader finds the file. You can get the contents, but not the absolute location.
You shouldn't want the absolute location. What will you do with it?
You can't write to the file. You can't alter anything. If your app is packaged in a WAR file, you can't alter its contents.
Im trying to get the file path of a document that is packaged as a resource in a jar file so that i can display it in a swing application. The way I have it now works when I run it from eclipse but if I export it to a runnable jar file I can't access the the documents that are packaged in the jar file. How can I get the file path of the document when its inside the jar file?
Here is the line of code showing how I am trying to access the document:
File document = new File(getClass().getResource("/resources/documents/document.pdf").getPath());
The only kind of "file path" that exists for something inside a JAR file is the path relative to the root of the JAR. But in your case it seems that you know it already (it's "/resources/documents/document.pdf"). Files inside a JAR file have no path that you can use to access them directly as they don't exist within the real file system. You need to use either getResource() or getResourceAsStream() to access them. I don't remember right now which classes are used for images in Swing, but look closely at those classes - they should have overloaded methods that accept something like InputStream or URL instead of file path.