JNI-wrapped library seeks out wrong working directory -- how to circumvent?
I am using JNI to wrap a few native functions in a closed-source PDF library. It has an dependent fonts
directory which must be in a subfolder of the calling application's directory. In my experience, it is standard to seek based on the current working directory. Thus, the problem.
When loading the JNI code into a Java application, the current working directory is correct. However, the calling application's directory is java.exe
's bin directory. I have verified that putting the dependent fonts
folder in C:\Program Files (x86)\Java\jre6\bin
folder works as expected.
The library seems to be using a C++ GetCommandLine()
call, or something similar to determine where the fonts directory should be. Obviously, this is an unacceptable solution.
I'd like to avoid calling an external EXE. But the only workarounds that I've come up with are:
- Compile an EXE, place in Java project directory, and use Java's
Runtime.exec()
to execute. (this does work) - Make JNI code launch a separate process which does the same as above (gains nothing but more complexity)
Any ideas on how I can circumvent this problem? When Java applic开发者_JS百科ations are compiled as a runnable JAR, is the resultant command line still the JRE's C:\Program Files\...java.exe
?
A Java executable maker can create an executable *.exe from your Java application without any native coding or compiling. You can put that executable, the jar files, the fonts and other application dependencies into a single install directory.
Exe4j is one of the executable makers that will support this, for Windows. It does not require any assumptions about the current working directory. This is important in the frequent case where you have no control over what the working directory is when the application is launched.
精彩评论