views:

46

answers:

2

Hi,

I'm making an executable JAR that uses the RxTx serial library. It requires access to rxtxSerial.dll and I want the JAR to put it there automatically. That way I don't need an installer -- just a standalone JAR. Can this be done?

When I call new FileOutputStream(new File("C:/Windows/rxtxSerial.dll")), I get the following exception:

java.io.FileNotFoundException: C:\Windows\rxtxSerial.dll (Access is denied)

Thanks,

Neal

+1  A: 

Yes you can put a DLL file in system folder. However, latest versions of Windows implements User Access/Account Control which can prevent you from doing so if you don't have enough priviliges. It does not matter what language or tool you are using to approach this though. In order for your Java application to be able to put files in C:\Windows, it should be ran as Administrator. But requiring user to run application as Administrator all the time might not be very convenient for the user. So what I'd suggest to do is to check if file is already there, if not, trigger UAC and copy it there. In this case Administrator privileges will be required only once. Here is a very good blog post by Mark S. Kolich on how to trigger UAC from Java.

Hope it helps.

Vlad Lazarenko
+2  A: 

Permissions will prevent you from writing to system directories in modern versions of Windows. Even if the system let you do it, it would not be good practice.

By packaging your app with One-Jar, you can bundle your native libraries inside the jar, and they will be expanded and added to the classpath at runtime.

It is easy to integrate into an Ant or Maven build. As a bonus, you can also include any dependent libraries and other resources within the jar, and they will all be expanded out to temp files at runtime.

In ant, repackaging your existing jar would look something like this:

<one-jar destfile="libraryWithNativeCode.jar">
    <manifest>
        <attribute name="One-Jar-Main-Class" value="${main.class}"/>
    </manifest>

    <main jar="libraryWithoutNativeCode.jar" />

    <binlib>
        <fileset dir="${bin.dir}" includes="rxtxSerial.dll"/>
    </binlib>
</one-jar>
James Van Huis