Are there tools to integrate Java and C++?
Asked Answered
V

5

7

thanks for reading this question.

I am doing this homework which need a GUI as frond end to integrate with back end code which written in C++.

I wanna to write this front end GUI in java as its cross-platform feature and strong graphic components.

Is there any good way I can integrate java and C++ well?

Thank you

Vandiver answered 3/12, 2009 at 21:55 Comment(0)
S
3

Swig works very well. It's a means to bind C/C++ to a huge variety of languages. I have experience of using this to talk to C++ with very little grief. Here's the manual page on using Swig and Java together. The tutorial gets you going very quickly, with many examples including Java.

I would however investigate splitting your application into a client/server architecture, to separate the C++ backend from the Java front end. You'll avoid the C++/Java development and integration pain = although you'll have to implement some communication protocol between the front and back end depending on requirements (e.g. basic sockets / webservice / HTTP+REST or possibly CORBA - which comes natively to Java and is designed for cross-language communication).

Sikhism answered 3/12, 2009 at 22:2 Comment(0)
G
2

Assuming you are the back end component is on the same machine you could use an interface layer as described by others

  • JNI
  • JNA
  • Swig
  • QTJambi

These all require you c++ backend to be available in a dll and usually provide Java proxies for C functions and sometimes c++ classes. There is a learning curve to all of these and some work to enable the Proxy.

Another approach would be to use a c++ process and communicate with this using either

  • command line
  • stdin/stdout

If you want to support communication across a network

  • sockets
  • CORBA
  • WebServices
  • Thrift

These also have a learning curve and some set up costs

Of these the command-line or stdin/stdout is probably the fastest to get working with the minimal amount of effort and knowledge. However it does not scale well to large interfaces as you must encode the input and output of each message as text

For the command-line approach you execute the c++ process using command line switches for the options, the results are either read from the processes standard out or its exit code.

For stdin/stdout you start the process each request is sent to stdin of the process and the results are read from stdout.

Gangplank answered 4/12, 2009 at 9:55 Comment(0)
N
1

Hava a look at JNI (Java Native Interface). Sun has an online book on JNI.

Nemato answered 3/12, 2009 at 21:58 Comment(0)
L
1

How about Thrift?

Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

Lecky answered 3/12, 2009 at 21:58 Comment(0)
P
1

If you are not writing the C++ backend library yourself, but just want to use a third-party library, the better alternative would be to use JNA.

The main benefit of using JNA over JNI in this case is that the bridging code is all written in Java (rather than in the native language, C++ in your case). This means you wouldn't need to complicate your build process by building C++ JNI interfaces, all your interface work would be written in the language of the main project.

If however, you are writing the C++ backend yourself, then any of the other options already provided would be equally applicable.

Pandorapandour answered 3/12, 2009 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.