Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java and C++  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
how can core java or normal java application communicate with C++ programs?

thanks,
bobby
 
author and iconoclast
Sheriff
Posts: 24218
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many ways!

1) One program can execute the other as a separate program, and they can interact with the other program's command line.

2) Two programs can communicate via a TCP socket.

3) A Java program can directly invoke C++ code right inside the Java program by using a tool called the Java Native Interface (JNI) (see here.)
 
Sheriff
Posts: 21289
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
2) Two programs can communicate via a TCP socket.


Shared memory, named pipes and files are also possibilities, although TCP is the easiest to use in Java while still avoiding the synchronization problems you'll get with the files (since Java may be able to read them even if the C++ is writing to them).
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
A Java program can directly invoke C++ code right inside the Java program by using a tool called the Java Native Interface (JNI) (see here.)




JNI works both ways: you can use it to call Java from C++, too.

Note that Java cannot be called without a JVM running to host the Java. However, JNI includes the Invocation Interface, for starting a JVM from C++.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot more possibilities - in one occasion, we use Windows Messaging Service, for example.

I think it would make more sense to come from the other direction: what are your requirements?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!