1

I am having problems with relative file paths that native functions use. When I call native C function from Java code, I get segmentation fault due to null file pointer. The only thing that works is to change these paths into absolute file paths, which is not solution for me. Is there any way to set root directory for native functions or to use Java project root folder to navigate through directories, or the absolute path is the only way?

3 Answers3

1

Java does not have a way to change the working directory.

This is because the Java developers consider changing the working directory to create more problems than it solves. specifically:

  • It would be global mutable state. Global mutable state makes it harder to isolate parts of the application from each other.
  • It would be prone to race conditions (another side effect of being global mutable state).
  • It would not have a significant benefit. Everything that you could do by changing the current directory you can already do with absolute paths.

Your C code will need to use absolute paths.

Alternatively, if you are willing to write additional C code, your C code could call the operating system's chdir function directly. This may be dangerous, as the JVM is not designed for this possibility.

Community
  • 1
  • 1
user253751
  • 57,427
  • 7
  • 48
  • 90
  • I am not lazy to specify absolute paths in C code, but problem is moving application to another PC. In Linux absolute path is "/home/user_name/...". What if I run application on another account? – Vladimir Djurdjevic Dec 30 '14 at 02:37
  • @VladimirDjurdjevic Why would using absolute paths mean hard-coding them? – user253751 Dec 30 '14 at 05:18
  • Thats what i taught before i knew i can make native functions and Java code communicate. I taught that only way is to do something with C code. I have never used JNI, or anything like it before, i am just student working on solo project, and i need to learn much more than they teach me at school :D Thank you anyway! – Vladimir Djurdjevic Dec 30 '14 at 11:59
0

I solved the problem! Solution is using File.getAbsoluteFilePath() function, and passing it to native C function. It can be used for finding absolute path of shared library also, which makes application platform independent. C code can navigate through it's directories like before bounding with Java code.

-1

Do you work in Linux? in Linux,you can add the path $LD_LIBRARY_PATH=.so file path to the ~/.bashrcfile and then reboot..

If you work with windows, you can put the .dll file in the path that as the .class file path.

luckyqiao
  • 44
  • 3
  • Yes i work in Ubuntu 14.04 currently, and I use System.load() function to load .so file so I can specify absolute library path. I am not sure I understand what you said. Does that mean that I can set directory that contains .so file for primary directory, and navigate relative to that directory? – Vladimir Djurdjevic Dec 30 '14 at 02:42
  • I don't :D I solved my problem already, but thank you anyway! :) – Vladimir Djurdjevic Dec 30 '14 at 14:28