Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to pass information from java to c++?

I have a java application I need to pass some info to a C++ program. It has been suggested that I use some simple socket programming to do this. Is this the best way? If not what are the alternatives? If so, how should I go about learning about socket programming?

like image 994
Alex Avatar asked Jan 20 '09 21:01

Alex


People also ask

Is it hard to go from java to C++?

Naturally, it would be easiest if the second course were also offered in Java, but learning to move from one language to another is a fact of life for today's software professionals. Fortunately, C++ has many features in common with Java, and it is easy for a Java programmer to gain a working knowledge of C++.

Can I use C with java?

Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa. Using JNI a java program has the capability to call the native C code.


2 Answers

You have a few options:

  • Pass a file from Java to C++. This is probably simplest. It's easy to test and shouldn't require any 3rd party libraries on either end.
  • Use sockets as mentioned. In C++, if you require a cross-platform solution a library such as ACE or boost will save you some heartache
  • Use JNI to call from Java to C++ or vice versa. This is probably the most difficult, but most performant.

For learning sockets, a Google search for "java socket tutorial" or "c++ socket tutorial" will give you lots of information.

like image 188
Dave Ray Avatar answered Oct 16 '22 04:10

Dave Ray


A simple way to do this is using standard input and output:

class MyTest {
    public static void main(String... args) {
        System.out.println("carpet");
    }
} // Java

#include <iostream>
#include <string>
int main() {
    string input;
    std::getline(std::cin, input);
    std::cout << "from java: " << input << std::endl; // output: carpet
} // C++

# start by piping java's output to c++'s input
$ java MyTest | ./my_receive 
like image 20
Johannes Schaub - litb Avatar answered Oct 16 '22 03:10

Johannes Schaub - litb