3

I'm testing a simple example of jni (on windows 7) by calling java functions in a c++ main.

I read that "relocation truncated to fit" error is due to a large project (not the case : just a main and a compiled java class named Sample with a static method intMethod(int)), linking object files in a different order (i only have one object file), or an expansive mapping scheme ... stack overflow thread

I use the following compiling command line: g++ -D __int64="long long" -I"C:\Program Files\Java\jdk1.7.0_55\include" -I"C:\Program Files\Java\jdk1.7.0_55\include\win32" -L"C:\Program Files\Java\jdk1.7.0_55\lib" -o sample sample.cpp -ljvm

and my main function:

#include <jni.h>
#include <cstring>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif

int main()
{
   JavaVMOption options[1];
   JNIEnv *env;
   JavaVM *jvm;
   JavaVMInitArgs vm_args;
   long status;
   jclass cls;
   jmethodID mid;
   jint square;
   jboolean no;
   options[0].optionString ="-Djava.class.path=.";

   //memset(&vm_args, 0, sizeof(vm_args));
   vm_args.version = JNI_VERSION_1_6;
   vm_args.nOptions = 1;
   vm_args.options = options;

   status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
   cls = env->FindClass("Sample");
   mid = env->GetStaticMethodID(cls, "intMethod", "(I)I");
   if(mid !=0)
    {  square = env->CallStaticIntMethod(cls, mid);
       printf("Result of intMethod: %d\n", square);
    }
   jvm->DestroyJavaVM();
   return 0;
}

The error is the following:

C:\Program Files\Java\jdk1.7.0_55\lib/jvm.lib(jvm.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol __imp_JNI_CreateJavaVM defined in section .idata$5 in C:\Program Files\Java\jdk1.7.0_55\lib/jvm.lib(jvm.dll.b)

error: collect2: ld returned 1 exit status

Can't seem to find a good answer anywhere about how to handle this error methodically!


-> Solution: Add -L"C:\Program Files\Java\jdk1.7.0_55\jre\bin\server" in compilation

Community
  • 1
  • 1
MarchToSuccess
  • 69
  • 1
  • 2
  • 11
  • Possible duplicate of [What does this GCC error "... relocation truncated to fit..." mean?](http://stackoverflow.com/questions/10486116/what-does-this-gcc-error-relocation-truncated-to-fit-mean) – Kalle Richter Oct 30 '15 at 20:36

2 Answers2

2

To clarify the solution edited into the question, the linker command line should include -L/path/to/jre/bin/server (which contains jvm.dll). It should NOT contain -L/path/to/jdk/lib (which contains jvm.lib).

I'm not familiar with conventional Windows development so I can't explain what's going on here. I assume the questioner was working on Cygwin like me.

Note that what you run the program, /path/to/jre/bin/server must also be on your PATH (serving the same function as LD_LIBRARY_PATH on Linux).

mhsmith
  • 6,675
  • 3
  • 41
  • 58
0

The truncation is referring to the stored relocation offset, not the symbol's label. I believe your solution might be found here.

Community
  • 1
  • 1
technomage
  • 9,861
  • 2
  • 26
  • 40