java.io.File relative path's not working after calling a native method.
843829Nov 1 2006 — edited Nov 6 2006Hi guys,
I have a doozy of a problem using JNI from within my Java application.
I do a lot of file work (using java.io.File) which uses file path's relative to the installation directory (if running from c:\foo\bar I will be accessing a directory "baz", which is in absolute terms in c:\foo\bar\baz).
I have several unit tests (thank god, otherwise I wouldn't have ever noticed this problem) that have all run successfully for months now (creating new files, moving files, deleting files etc.), but since we've added some JNI calls to an external C library these tests fail.
I have tried running my test-suite (TestNG, it really beats JUnit!) without the tests for the JNI code and they pass successfully (so I know my code works correctly). I have also tried running the suite over and over in a loop, and then tried calling the native methods at different points in the loop, but any operation I perform with a file after calling the native method fail.
I have also tried using relative and absolute paths for my -Djava.library.path pointing to where my DLL's are located, but this has no impact on it. I have also tried printing out the "user.dir" System property before and after the native method call (to see if the native code was moving the working directory for some reason) and they are the same.
I can reproduce this problem reliably (unfortunately this is a proprietary system, so I can't really release code to the public) and it is all caused by the native call (I've stripped everything else out to be only the file operations and the native code in a seperate test case).
I have a workaround, and that is to instantiate new File object's using the getAbsolutePath() of an already existing File object, however this does seem overkill (there is a lot of file processing going on, so I'm sure to miss something). Has anybody else had this problem, and managed to find a workaround that doesn't resort to changing all of my existing relative path code to use absolute paths?