1

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 applications are compiled as a runnable JAR, is the resultant command line still the JRE's C:\Program Files\...java.exe?

Courtney Christensen
  • 9,165
  • 5
  • 47
  • 56
  • I couldn't get GetCommandLine() to return the complete path so I was unable to test it, but did you try to make a symbolic link to java.exe in the correct directory and use it to launch your program? – Sergei Tachenov Dec 16 '10 at 18:06

1 Answers1

2

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.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • This sounds like workable solution; marking as accepted. I found a few open-source alternatives which create EXEs as well. Given the additional (unrelated) dependencies this project will have, I'm going to simply use `Runtime.exec` for the time being. This link about extracting an EXE from a JAR at runtime may be a useful reference -- http://stackoverflow.com/questions/600146/run-exe-which-is-packaged-inside-jar – Courtney Christensen Dec 17 '10 at 16:29