aminy
aminy

Reputation: 489

how to use java to implement C++ code

That is a phone interview question.

Assume you have a C++ code, you want to implement the code by using Java rather than rewriting the code. How would you solve that?

I cannot figure out this question. Can anyone told me the solution?(is it possible by import some package or java library?)

Thanks

Upvotes: 1

Views: 1089

Answers (1)

maxx777
maxx777

Reputation: 1330

Using Java Native Interface (JNI), you can use a method written in c++(or some other language) in your java code

  1. Begin by writing the Java program. Create a Java class that declares the native method; this class contains the declaration or signature for the native method. It also includes a main method which calls the native method.
  2. Compile the Java class that declares the native method and the main method.
  3. Generate a header file for the native method using javah with the native interface flag -jni. Once you've generated the header file you have the formal signature for your native method.
  4. Write the implementation of the native method in the programming language of your choice, such as C or C++. (this is what you are already given in this case).
  5. Compile the header and implementation files into a shared library file.
  6. Run the Java program.

Upvotes: 2

Related Questions