Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using the JNI with QT - Lib/DLLs are giving a file format error

Using the JNI with QT - Lib/DLLs are giving a file format error

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 4 Posters 9.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RBrNx277
    wrote on last edited by
    #1

    Hi there, I am currently trying to use the JNI with QT, to allow me to run some Java function inside my C++ application. I have added the correct INCLUDEPATH to the JNI.h and JNI_md.h files which is:

    INCLUDEPATH += "C:/Program Files/Java/jdk1.8.0_65/include/"
    INCLUDEPATH += "C:/Program Files/Java/jdk1.8.0_65/include/win32/"
    

    I have also adds a LIBS flag to the .pro file to tell QT where the JNI dll is located which looks like the following:

    LIBS += "C:/Program Files/Java/jre1.8.0_65/bin/server/jvm.dll"
    

    However, once I attempt to build my program I get an error saying "file not recognized: File format not recognized" on the .DLL file. Has anyone else got the JNI to work with QT? Am I doing something wrong?

    Thanks, Conor

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, are you building a 64-bit Qt program? Otherwise, the error could be because mixup of 32-bit and the 64-bit jvm.dll.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RBrNx277
        wrote on last edited by
        #3

        I'm not sure, is there a way to check?

        1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          Sure (assuming you are using shadow builds) you can look at the directory name for your program's .exe file, something like "build-myprogram-Desktop_Qt_5_5_1..." it should say either 32bit or 64bit.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RBrNx277
            wrote on last edited by
            #5

            I had a look and I'm building a 32 bit application. I would imagine that the DLL is 32bit as well. Unless you know of a way to check?

            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              Easiest is to check if you have a 32bit or 64bit installation of Java, open a CMD window and type:
              "C:\Program Files\Java\jdk1.8.0_65\bin\java.exe" -version

              You should see 3 lines, the last one tells if you have 32bit or 64bit Java.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RBrNx277
                wrote on last edited by
                #7

                It gave me this response: http://i.imgur.com/WDCfFhx.png
                So I guess I do have the 64bit JNI? I'll have a look to see if there is a 32bit version. Or would it be easier to build my application as 64bit?

                Thanks for your help.

                1 Reply Last reply
                0
                • hskoglundH Offline
                  hskoglundH Offline
                  hskoglund
                  wrote on last edited by
                  #8

                  Well 64-bit is the future :-) So I think try building your app as 64-bit.
                  However on Windows that requires you to have Visual Studio 2013 installed (there's no 64-bit option for the MinGW compiler :-(

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RBrNx277
                    wrote on last edited by
                    #9

                    Ahh OK that makes sense. I have Visual Studio 2013 installed, is there a step by step process to compile my QT application for 64bit?

                    1 Reply Last reply
                    0
                    • hskoglundH Offline
                      hskoglundH Offline
                      hskoglund
                      wrote on last edited by
                      #10

                      It's pretty straightforward: first run Qt's MaintenanceTool.exe app to add 64-bit Visual Studio 2013.
                      Then to select 64-bit build for your project, easiest is to remove the file ending with "pro.user" (like myprogram.pro.user) and next time you start Qt Creator you should be able to select the 64-bits build.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        RBrNx277
                        wrote on last edited by
                        #11

                        OK so I haven't tried to build the 64bit application yet, but I did try to download and use a 32bit Java version. I updated the .pro file to use the new Include Paths and LIBS flag:

                        INCLUDEPATH += "C:/Program Files (x86)/Java/jdk1.8.0_74/include/"
                        INCLUDEPATH += "C:/Program Files (x86)/Java/jdk1.8.0_74/include/win32/"
                        LIBS += "C:/Program Files (x86)/Java/jre1.8.0_74/bin/client/jvm.dll"
                        

                        The "file format not recognized" error seems to have disappeared (yay?) but now I get the following error:

                        undefined reference to `_imp__JNI_CreateJavaVM@12'
                        

                        I assume I'm still doing something incorrectly? My guess is using the 64bit application will also produce this error?

                        I seen a few bits and pieces online about .Lib files and .a files, are they something I need?

                        Thanks

                        1 Reply Last reply
                        0
                        • hskoglundH Offline
                          hskoglundH Offline
                          hskoglund
                          wrote on last edited by
                          #12

                          Yeas you have now opened another nice can of worms. For starters you'll need a jvm.lib, here's an example Good luck!

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            RBrNx277
                            wrote on last edited by
                            #13

                            Thanks for the link, it doesn't do anything differently from the way I have everything coded at the moment though. I have all the same (or similar) code to set up the JVM and call Java functions, but my project wing build because of that undefined reference error. It doesn't seem to mention anything about .lib/.a files either, do I just need to add the location of the JVM.lib to the libs flag in the .pro file? Or is there more to it?

                            1 Reply Last reply
                            0
                            • jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Shouldn't this one

                              LIBS += "C:/Program Files (x86)/Java/jre1.8.0_74/bin/client/jvm.dll"
                              

                              either be

                              LIBS += -L"C:/Program Files (x86)/Java/jre1.8.0_74/bin/client" -ljvm"
                              

                              But it probably will not work in any case because you use MinGW and JRE most probably another (VS?) compiler. On Windows you cannot mix compilers.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • R Offline
                                R Offline
                                RBrNx277
                                wrote on last edited by
                                #15

                                Aren't the -L flags only used in Linux systems? Or did I read that wrong?
                                OK so the problem might be the JVM.dll was compiled with a VS compiler and I'm trying to use it with the MinGW compiler? Is there a way to check what compiler the DLL was compiled with? Could I change the Compiler QT is using to the same one?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi,

                                  No they are not.

                                  The first problem you have is that you are trying to link against a .dll which is wrong. You need the corresponding .lib file.

                                  But first thing, do you need that for an Android application ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  2
                                  • R Offline
                                    R Offline
                                    RBrNx277
                                    wrote on last edited by
                                    #17

                                    Ok, I know the location of the .lib file, what should I do with it?

                                    No this is just a regular windows application. There's a piece of code I'd like to use which is written in Java, I will perhaps look to convert it to C++ in the future but for now I'd just like to use the exisiting Java code.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      Follow the example that @jsulm provided:

                                      LIBS += -L"C:/Path/to/Java/libs/" -ljvm"

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      1
                                      • R Offline
                                        R Offline
                                        RBrNx277
                                        wrote on last edited by
                                        #19

                                        Nice one, thank you! Finally managed to get it working by only using these three lines in my .pro file

                                        INCLUDEPATH += "C:/Program Files (x86)/Java/jdk1.8.0_74/include/"
                                        INCLUDEPATH += "C:/Program Files (x86)/Java/jdk1.8.0_74/include/win32/"
                                        LIBS += -L"C:/Program Files (x86)/Java/jdk1.8.0_74/lib/" -ljvm
                                        

                                        and also by placing the JVM.dll where the .exe file is built by QT.

                                        Unfortunately my program now crashes when it attempts to create a VM for Java and says

                                        Error occurred during initialization of VM
                                        Unable to load native library: Can't find dependent libraries
                                        

                                        But I guess that's a different problem!

                                        1 Reply Last reply
                                        0
                                        • jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          It looks like the jvm DLL depends on at least one more DLL. You can use DependencyWalker to check which dependencies you have.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          R 1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved