Is it possible to include a dll in a jar with eclipse?

1

I have an application that uses a jar. This jar depends on a dll, so I included the dll in a folder inside the eclipse project. The problem is that when I export the project with "Runnable jar file- > Package required libraries into generated JAR" it only exports the jar without the dll. Is there any way to do it correctly?

Important: I would like to distribute the packaged dll in the jar. It is more than likely that the users of my application do not have the installed dll.

    
asked by abrahamhs 18.03.2016 в 21:20
source

2 answers

2

Given the characteristics of your scenario:

  • Project created in Eclipse
  • The project is pure Java type, without using Ant, Maven, Gradle or similar

The easiest way is to create an additional folder in your project, let's call it lib , place the DLL inside lib and then configure lib to be a folder that contains sources in the project. Consider that when you export a project as JAR, what Eclipse will do is pack all the components that are in the source code folders in the JAR. Here is an example of easy understanding.

  • In Eclipse, we created a Java-like project called TestAddFile
  • At the root of the project, we created the lib folder
  • In the lib folder, we create a text file called data.txt. Open the file and put as content "Hello world!" without quotation marks. Save it and close it.
  • In the source folder, we create a package and inside it a class that will be used to verify that the file was packaged with the JAR and that it can be accessed from within the Java classes. In this class the following code is added (package name and class are irrelevant for the example):

    //JDK mínimo: 1.7. Si utilizas una versión anterior, adapta el código
    public static void main(String[] args) {
        //se prueba el acceso al archivo datos.txt desde dentro del jar
        try (BufferedReader br = new BufferedReader(new InputStreamReader(Main.class.getClassLoader().getResource("datos.txt").openStream()))) {
            System.out.println(br.readLine());
        }catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
    
  • Right click on the lib folder, select Build Path / Use as Source Folder

  • Export your Java project as Runnable Jar (Executable Jar). Give it as a name test-add-file.jar

  • Run the jar from your console. You should have a result similar to this:

  • For your particular case, instead of using a datos.txt file, you will use your DLL or any other resource you need in your project and be packaged along with the sources in your JAR.

        
    answered by 19.03.2016 в 07:11
    0

    You can register again the path where you have your .DLL file. In the menu, option Run > Run Configurations... > Arguments , add the path that contains your .DLL

        
    answered by 18.03.2016 в 22:38