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

后端 未结 8 1756
太阳男子
太阳男子 2021-02-07 04:59

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

相关标签:
8条回答
  • 2021-02-07 05:07

    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 
    
    0 讨论(0)
  • 2021-02-07 05:10

    The options that Dave mentioned are some of the most common approaches.

    As an extension to the JNI solution, you might also look at direct ByteBuffers (Java 1.4 and onward) if you have some raw data you want to share between Java and C++. The direct byte buffers allow you to allocate a buffer of some size on either the C++ or Java side and then easily access the buffer from both languages; in Java this is done through methods on the ByteBuffer object and in C++ you can obtain a pointer to the data. The ByteBuffers are handy if you have a decent amount of information to exchange and you don't want to pass it all as parameters in a JNI method.

    The socket approach is probably overkill, but I'm not sure what you're specifically trying to do.

    0 讨论(0)
  • 2021-02-07 05:19

    There's also the option of SOAP but that's probably overkill assuming you've simply got two processes on the same machine. Still I mention it for sake of completeness.

    0 讨论(0)
  • 2021-02-07 05:21

    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.

    0 讨论(0)
  • 2021-02-07 05:27

    More information needed (but a good question).

    Are these two separate programs running at the same time?

    If not, and the Java program needs to call a C++ library, the JNI answer is a good solution, but it won't really work across processes.

    In the case of processes, you really do need to find a way to communicate across processes...a simple solution (probably not the best, but I don't know your exact situation) would be to have a binary file that is written to and read from...just don't forget to beware of race conditions! Sockets could also work, but if your programs communicate a lot, it might be a bit of a hog on CPU power.

    0 讨论(0)
  • 2021-02-07 05:30

    Although an answer has been accepted, a key problem here is the semantic mismatch between the two languages: assuming you have some sort of stream connection going on, how do you efficiently interchange data?

    One solution I might recommend is Google Protocol Buffers. It doesn't solve the subprocess/JNI/socket issue, but what it does do is allow you a pretty efficient way to have a complex object structure transit over a stream protocol.

    Remember: JNI is complicated, particularly for complex object references, but it's efficient. The most important thing is to pass data over that efficient context switch properly. GPB+JNI/Sub-Process Socket allows a single context switch to pull data over, combined with efficient codec support on both sides.

    0 讨论(0)
提交回复
热议问题