Pregunta

Goal

I'm currently developing an application in Netbeans using the Netbeans Platform on Windows (a cross-platform solution would be wonderful but something hacky for Windows if required is fine for now). The application communicates with a native C++ DLL through JNA. The DLL sits in a specified directory which I cannot control or change - say C:\DLL.

The DLL itself loads some files for reading in values etc. but it does this using relative paths. So it requires the "current" directory to be C:\DLL. Again, this is something I can't change in this project. The DLL is something I have to communicate with as is (unfortunately).

Problem

For regular JAVA applications I've had the option in Netbeans to set the working directory of the launched application like shown below:

http://i.imgur.com/3HuQKS8.png

But in the Netbeans Platform framework/template there are no such options. For the most part it looks like Netbeans just makes the directory of the current file I have focused in the Editor pane to be the Current Working Directory.

So how do I go about doing this? I might be able to run the application through a shortcut that sits in C:\DLL but this doesn't help when I'm debugging the application through Netbeans.

I want to know how I can run/debug this Netbeans Platform application with the current working directory set.

Clarifying Netbeans Platform The Netbeans Platform is an application framework of sorts. Has a pre-defined structure based on "modules" that interact to form the full application. The whole thing is hosted inside the Netbeans Platform environment which provides each of your modules with tabbed/docking windows. Kind of nice to develop larger applications.

More information here: https://netbeans.org/kb/trails/platform.html

¿Fue útil?

Solución

I've resorted to using chdir as @technomage (I can't seem to upvote his/her comment with my current account status) has also suggested.

To do this, I used the following piece of code before I load up my C++ DLL through JNA.

NativeLibrary clib = NativeLibrary.getInstance(null);
int result = clib.getFunction("_chdir").invokeInt(new Object[]{"<PATH GOES HERE>"});

Source: https://www.java.net/node/643965#comment-821128

You can also check the result by checking the result variable. Should be zero if everything went well.

I am not quite sure why null works for NativeLibrary.getInstance. The documentation doesn't say anything specifically about this and I haven't been able to glean anything from the source here: https://github.com/twall/jna/blob/master/src/com/sun/jna/NativeLibrary.java - But passing null does seem to get you the default libc or equivalent for your platform.

Note also that I had to add an _ (underscore) to the function name. This has to do with how function calls get mangled when compiled on Windows. As far as I know, this isn't required on other platforms, but I don't have the ability to test this right now.

Since I was unsure about whether my call was actually working, I did the following first:

Function f = clib.getFunction("_chdir");

This returns a function "pointer" f that you can trace/debug to see if you have a valid reference. Luckily, in my case, all I had to do was add the underscore as was suggested in the source link above.

After this call to chdir, the C++ DLL I need to access has been happily accessing files relative to the location specified in chdir.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top