0

I have two seperate codes one is written in c++ and the other is in java. Now what I want to do is ,First I want to run c++ code, and then I want to give the output file of this program to the second program, writen in java, as an input. I need to do this fully automatically. Is there a possible way to do this ?

I mean, I'll type a single command into terminal and what I`ve explained above will occur.

Do I need to write a shell script for that ?

9
  • By the way, I work on ubuntu 12.04 LTS but It would be better what I`ll do support cross platform. Commented Dec 15, 2014 at 6:34
  • You can use Java Native Invocation, but shell script would be much easier.
    – Everv0id
    Commented Dec 15, 2014 at 6:35
  • JNI is one option but I never liked it.
    – Ashwani
    Commented Dec 15, 2014 at 6:36
  • @Everv0id, thanks for the reply, I dont have any shell script experience, does it be so hard for me ? Commented Dec 15, 2014 at 6:36
  • Why JNI? Does it really matter which programming languages you are using, which is first or second? All you need to have is 2 executables ready and then use shell scripting, "pipe" forward the output from one program to other..
    – user1
    Commented Dec 15, 2014 at 6:37

2 Answers 2

1

Does it really matter which programming languages you are using, which is first or second? All you need to have is 2 executable ready and then use shell scripting, use "pipe" to forward the output from one program to other..

e.g. ls -l | sed -e "s/[aeio]/u/g"

Update:

cppprogram1 | java program2 cppprogram1.txt

cppprogram1.txt is output file generated by cppprogram1.

5
  • since I dont have any experience with shell, I have to ask this, the line of code provided by you above does not do that what I want right? It is just an example Commented Dec 15, 2014 at 6:43
  • No ofcourse not, this is an example how you can use pipe. "|"
    – user1
    Commented Dec 15, 2014 at 6:44
  • Why such troubles? At least, Java allows to use < and > operators to redirect System.in and System.out. I suppose cin and cout in c++ can be used similar way.
    – Everv0id
    Commented Dec 15, 2014 at 6:45
  • @Everv0id, You haven't understood what OP is asking!
    – user1
    Commented Dec 15, 2014 at 6:48
  • @user1 sorry, you are correct. Pipes solve problem completely.
    – Everv0id
    Commented Dec 15, 2014 at 7:00
-1

Depends on your situation. If you just want to throw the output of a executable, use pipe. The C++ binary would output to stdout, and the java file would read from stdin. If you need something more sophisticated, you probably need to use one of those serialization libraries like Thrift.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.