Relative path using Level hierarchy(../../) - java

I want to use the relative path in xml files in our project. I have the files in the following location.
D:/SDC-Builds/SRDM2.3.0/SRDM/Svr/IdP/IdPserver/conf/attribute-r.xml
I have other xml file which needs to ref the above location, I use the following relative path to be independent of machines and folder names.
In D:/SDC-Builds/SRDM2.3.0/SRDM/Svr/IdP/IdPserver/others/service.xml, i am using the code like below
service.xml
<srv:ConfigurationResource="../../../../../../IdP/IdPserver/conf/attribute-r.xml">
</srv>
Please tell me am i using proper convention to refer the attribute-r.xml ?

If your Project Root Directory is SRDM, then you need to get back from your executable path.
Say you have your exe file at SRDM/Svr/bin/EXECUTABLE.exe then,
you need to mention in xml as
<srv:ConfigurationResource="../IdP/IdPserver/conf/attribute-r.xml"></srv>
ie. Current working directory is SRDM/Svr/bin/ and from that you need to get back up to common junction[Svr] in your case.

Related

How to Reference Local XSD File By Relative Path

This is a follow up on How to Configure Eclipse to Work with `ehcache` and there is an example of absolute path in How to reference a local XML Schema file correctly?
Since the xsd file is not longer being hosted, I downloaded a copy of it from the archive. I've put the file in my resource directory and would like to reference it in my xsi:schemaLocation from a relative path of my project. We have both windows and mac developers, so an absolute path convention like file:///c:/project/foo won't work.
Another alternative to relative path would be if there is a way to reference system properties for both windows and mac, then I could do 2 entries like file:///$HOME/workspace/foo.
My project on my mac exists in ~/workspace/foo and echo of $HOME gives my home path.
My spring-cache.xml exists in ./src/main/webapp/WEB-INF/spring/
My ehcache-spring-1.2.xsd exists in ./src/main/resources/
The following are some that I've tried without success:
file:///$HOME/workspace/foo/src/main/resources/ehcache-spring-1.2.xsd
file:///./src/main/resources/ehcache-spring-1.2.xsd
file:///../../../../resources/ehcache-spring-1.2.xsd
file:///../../../../resources/ehcache-spring-1.2.xsd
../../../../resources/ehcache-spring-1.2.xsd
file://../../../../resources/ehcache-spring-1.2.xsd
file://$HOME/workspace/foo/src/main/resources/ehcache-spring-1.2.xsd
file://./src/main/resources/ehcache-spring-1.2.xsd
file://../../../../resources/ehcache-spring-1.2.xsd
I also seem to have issues getting it to reference the file from absolute path too, so perhaps there is a different way to reference the file on mac? e.g. The following did not work:
file:///Users/me/workspace/foo/src/main/resources/ehcache-spring-1.2.xsd
file:////Users/me/workspace/foo/src/main/resources/ehcache-spring-1.2.xsd
file://Users/me/workspace/foo/src/main/resources/ehcache-spring-1.2.xsd
Think my issue was that the webapp dir may not have visibility to the resources folder.
I say this because based on the reading of the following stack overflow questions, it looks like the relative path stuff should be pretty straigh forward: XML File with local copy of XML Schema and How to use Schema that is on local machine in XML document
I then tried to brute force the references, in case it was something with deploy directory or class path, and all of the following failed:
../../../../resources/ehcache-spring-1.2.xsd
../../../resources/ehcache-spring-1.2.xsd
../../resources/ehcache-spring-1.2.xsd
../resources/ehcache-spring-1.2.xsd
./resources/ehcache-spring-1.2.xsd
../ehcache-spring-1.2.xsd
./ehcache-spring-1.2.xsd
The solution:
However by moving the xsd file from ./src/main/resources/ to ./src/main/webapp/WEB-INF/spring/ I was then able to reference it correctly/easily by just ehcache-spring-1.2.xsd

How do I configure the server so that It stores the dynamically created files in the specified folder not under the bin folder?

I have developed a web application which will create an xml file based on the user Input. It has some more functionality. I configured my application to store the xml file(s) in the project root folder. When I run it through eclipse It stores in the specified location. But If I manually put the war file in apache tomcat and run the application those newly created xml files are going into bin directory since I used relative path. Now I dont want it to be created under bin directory. I want those files to be created somewhere local in the system. Is there any way to do it ? Or else what is the best way to deal with those xml files. I am using spring MVC.
You could use a variable in your code would contain the first part of the path (the real location where you want to write the file). And inject the value of that variable from a properties file, that way you wouldn't hardcode anything, and still would be able to provide only the relative path in your code.

relative path in Java

My program has to use certain files that reside in another directory. Right now I am using absolute path to specify the location of the files. But I know that the relative position of these files will remain the same compared to where my program is residing. How I can use relative path in Java to specify the location of these files?
For example my program is residing in
/home/username/project/src/com/xyz/
..and the target files are in..
/home/username/project/secure/
For example my program is residing in /home/username/project/src/com/xyz/
and the target files are in /home/username/project/secure/
Knowing the place where your program's source code resides does not help. What you need to know is the current directory of the program when it is executed. That could be literally anything. Even if you are launching the application from (for example) Eclipse, the application launcher allows you to specify the "current directory" for the child process in the Run configuration.
Your current path.
currentPath= /home/username/project/src/com/xyz/;
Relative path to "/home/username/project/secure/" folder is
relativePath= ../../../secure;
In Java you can use getParentFile() to traverse up the tree.
File currentDir = new File(".");
File parentDir = currentDir.getParentFile();
This will be safe as you are not using system dependent file separator (../)

Java (maven web app), getting full file path for file in resources folder?

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

Absolute Path of Project's folder in Java

Lots of confusion in this topic. Several Questions have been asked. Things still seem unclear.
ClassLoader, Absolute File Paths etc etc
Suppose I have a project directory structure as,
MyProject--
--dist
--lib
--src
--test
I have a resource say "txtfile.txt" in "lib/txt" directory. I want to access it in a system independent way. I need the absolute path of the project.
So I can code the path as abspath+"/lib/Dictionary/txtfile.txt"
Suppose I do this
java.io.File file = new java.io.File(""); //Dummy file
String abspath=file.getAbsolutePath();
I get the current working directory which is not necessarily project root.
Suppose I execute the final 'prj.jar' from the 'dist' folder which also contains "lib/txt/txtfile.txt" directory structure and resource,It should work here too. I should absolute path of dist folder.
Hope the problem is clear.
You should really be using getResource() or getResourceAsStream() using your class loader for this sort of thing. In particular, these methods use your ClassLoader to determine the search context for resources within your project.
Specify something like getClass().getResource("lib/txtfile.txt") in order to pick up the text file.
To clarify: instead of thinking about how to get the path of the resource you ought to be thinking about getting the resource -- in this case a file in a directory somewhere (possibly inside your JAR). It's not necessary to know some absolute path in this case, only some URL to get at the file, and the ClassLoader will return this URL for you. If you want to open a stream to the file you can do this directly without messing around with a URL using getResourceAsStream.
The resources you're trying to access through the ClassLoader need to be on the Class-Path (configured in the Manifest of your JAR file). This is critical! The ClassLoader uses the Class-Path to find the resources, so if you don't provide enough context in the Class-Path it won't be able to find anything. If you add . the ClassLoader should resolve anything inside or outside of the JAR depending on how you refer to the resource, though you can certainly be more specific.
Referring to the resource prefixed with a . will cause the ClassLoader to also look for files outside of the JAR, while not prefixing the resource path with a period will direct the ClassLoader to look only inside the JAR file.
That means if you have some file inside the JAR in a directory lib with name foo.txt and you want to get the resource then you'd run getResource("lib/foo.txt");
If the same resource were outside the JAR you'd run getResource("./lib/foo.txt");
First, make sure the lib directory is in your classpath. You can do this by adding the command line parameter in your startup script:
$JAVA_HOME/bin/java -classpath .:lib com.example.MyMainClass
save this as MyProject/start.sh or any os dependent script.
Then you can access the textfile.txt (as rightly mentioned by Mark) as:
// if you want this as a File
URL res = getClass().getClassLoader().getResource("text/textfile.txt");
File f = new File(res.getFile());
// As InputStream
InputStream in = getClass().getClassLoader()
.getResourceAsStream("text/textfile.txt");
#Mark is correct. That is by far the simplest and most robust approach.
However, if you really have to have a File, then your best bet is to try the following:
turn the contents of the System property "java.class.path" into a list of pathnames,
identify the JAR pathname in the list based on its filename,
figure out what "../.." is relative to the JAR pathname to give you the "project" directory, and
build your target path relative to the project directory.
Another alternative is to embed the project directory name in a wrapper script and set it as a system property using a -D option. It is also possible to have a wrapper script figure out its own absolute pathname; e.g. using whence.

Categories