views:

243

answers:

5

Hello, 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

+1  A: 

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

Asaph
+1  A: 

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.

jldupont
+1  A: 

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).

Brian Agnew
+1  A: 

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.

Clinton
Note that JNA only works for libraries with a C API.
Michael Brewer-Davis
A: 

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.

iain