-3

I'm using Mozilla Rhino for my application and I need to call on a C/C++ library during implementation. Is there any easy way to do this other than calling on c/C++ functions through java directly?

waynchi
  • 144
  • 1
  • 7

1 Answers1

2

You won't be able to call C / C++ functions directly from Javascript.

You could create a Java wrapper class that can call C / C++ native code from Java. Then call methods on the Java wrapper class from Javascript. If you want to take that approach, search for a tutorial on using JNA or JNI or similar (for Java <-> C / C++).

However, there are a few problem with this approach:

  • The C / C++ code will be running in the same address space as the JVM. Bugs in the C / C++ code are liable to hard crash the JVM. Hard crashes are difficult to debug.

  • Typical C / C++ code is not thread-safe. If you try to call it from Java code that is multi-threaded, bad things can happen; e.g. see above.

  • The Javascript -> Java -> C++ calling sequences are not going to be cheap. It is not unknown for people to try using C / C++ to get a performance boost, only to find that it makes their application run slower.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216