2

I have written some code that relies on another project that I have included in my build path. The issue I am having is that the other project has a reference to some XSL files and they are being accessed using a relative path ./xlst/ .

I had dealt with that on my local workspace by simply copying the xlst folder into my project. At that time all was gravy. However, I have now JARred this up and put it onto a server, I don't think it is going to look inside the JAR for a relative path!

Here is what you will find in the error log to clarify (note the path):

ERROR:  'Could not compile stylesheet'
FATAL ERROR:  'Could not compile stylesheet'
           :c:\logs\.\xslt\Transform.xsl (The system cannot find the path specified)

My Question is this: I see that it automatically started looking in C:\logs (aka Current Working Directory) and then appended the relative path, can I force it to start somewhere other than c:\logs so it does look in a proper folder where I can drop in the XSL files?

Edit: I tried adding a File f = new File("c:/myPathToXsltFolder/") just before my call to the other project method to see if it would affect the "Current Working Directory" path but it didn't seem to affect it.

P.S. I need to do this without modifying the other project, so unable to use suggestion to change relative path to getClass().getResource(name).

Kairan
  • 5,342
  • 27
  • 65
  • 104
  • Have you seen this question/answer http://stackoverflow.com/questions/3844307/how-to-read-text-file-from-relative-path-in-a-project – Mauricio Gracia Gutierrez Jun 15 '15 at 15:24
  • @MauricioGracia Thanks for link, I reviewed the answer to that question which says to change the reference of relative path to getClass().getResource("ListStopWords.txt"); - however that requires me to modify the OTHER project code, which I cannot do. However, now that I know the terminology is "Current Working Directory" I can review some other post like http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java – Kairan Jun 15 '15 at 15:57
  • please share some of your code, because when you say "before calling the other project method is not clear what you mean – Mauricio Gracia Gutierrez Jun 15 '15 at 16:22
  • I was able to set working directory like this in code: System.setProperty("user.dir", wd); and I added that line just before the call to the method in the other project that contains a relative path. This allowed the relative path to resolve to the location that works for me! – Kairan Jun 15 '15 at 16:26

1 Answers1

0

All paths in Java are relative to the user.dir property that you can set when you start the JVM with -Duser.dir=<path>. Nevertheless this affects the entire program and all paths will be relative to the path you specify instead of c:\logs like you say.

Using relative paths is not a very good idea so most thing use configurable properties or resources and changing user.dir probably will not affect anything else. But since you put things onto a server then that server (if it's a java web server and not simply a server machine) may expect a certain layout under the user dir. In this case, you have to test for problems, I guess.

m4ktub
  • 3,061
  • 1
  • 13
  • 17
  • I agree. I think this was the solution I was looking for. I have found some info that I can also use in my Java code to set the path just before calling the external method in the other project. http://www.javacodex.com/Files/Set-The-Current-Working-Directory – Kairan Jun 15 '15 at 16:25
  • It's true that system properties can be set at runtime but if you do then you loose the ability to look at the command line and know how the JVM is configured. That's useful when configuring deployments and other sysadmin tasks. If you do everything then it's less relevant until sometime passes and you become a stranger to your setup. :) – m4ktub Jun 15 '15 at 17:55