Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
hi every one!

i have 2 applications one on eclipse and the other on VS2010C++, so i want to send parameters from the java code to the other, i just konw how to execute the c++ code from the java code.
what i want is this : send paramters from java code via a methode( not from the keybord) to the c++ one after that exécute the c++ code and get the result from it, so here is my java code :

Java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
class Program
{
    private final Process proc;
    private final Thread out, err, in;
    public Program(String cmd, OutputStream pOut, OutputStream pErr, InputStream pIn) throws IOException
    {
        proc = Runtime.getRuntime().exec(cmd);
        out = new Transfert(proc.getInputStream(), pOut);
        err = new Transfert(proc.getErrorStream(), pErr);
        in = new Transfert(pIn, proc.getOutputStream());
        System.out.println("le output de lapplication ="+ out);
        System.out.println("le input de lapplication ="+ in);
        System.out.println("l'erreur de lapplication ="+ err);
        out.start();
        err.start();
        in.start();
    }
    public void kill()
    {
        out.interrupt();
        err.interrupt();
        in.interrupt();
        proc.destroy();
    }
}
class Transfert extends Thread
{
    private final InputStream in;
    private final OutputStream out;
    public Transfert(InputStream in, OutputStream out)
    {
        this.in = in;
        this.out = out;
    }
    @Override
    public void run()
    {
        Scanner sc = new Scanner(in);
        try
        {
            while (sc.hasNextLine())
            {
                out.write((sc.nextLine() + System.lineSeparator()).getBytes());
                out.flush();
                if (isInterrupted())
                    break;
            }
        }
        catch (IOException e)
        {
            System.err.println(e);
        }
        sc.close();
    }
}
public class Test
{
    public static void main(String[] args) throws Exception
    {
        Program prog = new Program("C:\\Division.exe", System.out, System.err, System.in);
      //  prog.kill();
    }
}


and here is my c++ code :
C++
#include <iostream>
int main()
{
    int a,b;
    std::cout << "a = ";
    std::cin >> a;
    std::cout << "b = ";
    std::cin >> b;
    if (b==0)
        std::cerr << "Division par zero interdite" << std::endl;
    else
        std::cout << "a/b = " << (a/b) << std::endl;
    return 0;
}


Could you help me with that ???

Thank you very much
Posted

I did this once using Glassfish (java side) to a WCF service.

Mind you this was web server to web server using soap 1.2 and that's probably overkill for your requirement.

What type of IPC (interprocess communications) is available from a java process to a windows process? I realize java runs in it's own "sandbox" - and I'm not a java guy.

If you do a google on "java application ipc to c++" you should find your answer.
 
Share this answer
 
1. Don't use scanner when transferring data between an InputStream and an OutputStream. There is no need for any kind of conversion. Read binary byte array and write the same.
2. When redirecting program stdin its a good practice to close the input of the program when you don't want to send more input to it.
3. Use PipedOutputStream/PipedInputStream to create a "channel" to simulate input.

Java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;

class Program
{
    private final Process proc;
    private final Thread out, err, in;
    public Program(String cmd, OutputStream pOut, OutputStream pErr, InputStream pIn) throws IOException
    {
        proc = Runtime.getRuntime().exec(cmd);
        out = new Transfert(proc.getInputStream(), pOut, false);
        err = new Transfert(proc.getErrorStream(), pErr, false);
        in = new Transfert(pIn, proc.getOutputStream(), true);
        System.out.println("le output de lapplication ="+ out);
        System.out.println("le input de lapplication ="+ in);
        System.out.println("l'erreur de lapplication ="+ err);
        out.start();
        err.start();
        in.start();
    }
    public void kill()
    {
        out.interrupt();
        err.interrupt();
        in.interrupt();
        proc.destroy();
    }
}

class Transfert extends Thread
{
    private final InputStream in;
    private final OutputStream out;
    private boolean closeOutput;
    public Transfert(InputStream in, OutputStream out, boolean closeOutput)
    {
        this.in = in;
        this.out = out;
        this.closeOutput = closeOutput;
    }
    @Override
    public void run()
    {
        try
        {
            byte[] buf = new byte[0x100];
            do
            {
                int len = in.read(buf);
                if (len < 0)
                    break;
                out.write(buf, 0, len);
                out.flush();
            }
            while (!isInterrupted());
        }
        catch (IOException e)
        {
            System.err.println(e);
        }
        finally
        {
            if (closeOutput)
            {
                try
                {
                    out.close();
                }
                catch (IOException ex) {}
            }
        }
    }
}

public class Test
{
    public static void main(String[] args) throws Exception
    {
        //Program prog = new Program("C:\\Division.exe", System.out, System.err, System.in);

        PipedInputStream pipedInput = new PipedInputStream();
        PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput);
        Program prog = new Program("C:\\Division.exe", System.out, System.err, pipedInput);

        // TODO: Write pipedOutput from your favorite thread to send input to the program and finally close the stream.
        // I will use a PrintWriter because we are working here with text and not binary data.
        PrintWriter pw = new PrintWriter(pipedOutput);
        // println outputs platform specific newline but pw.print("5\n") would also convert "\n" to platform specific newline.
        pw.println("5");
        pw.println("2");
        pw.close();

      //  prog.kill();
    }
}
 
Share this answer
 
v2
Comments
Manel1989 22-Sep-13 6:24am    
Thank you , alll 5555555555555555555555555 :)
Manel1989 21-Oct-13 19:25pm    
hi
i have a question :is it possible to send a whole vector in one chut not sending every parameter of that vector alone ???? because sending like this take much time to snd and to recive to answer so i thought it may be esayer if i send a whole vector together??????????

what do you think of that . Thanks for every help
You would be better making the C++ code into a DLL and using the Java Native Interface[^] to access it.
 
Share this answer
 
Comments
H.Brydon 22-Sep-13 23:06pm    
Yeah, my thoughts exactly. +5
Richard MacCutchan 23-Sep-13 3:23am    
Thanks, I wonder about some of the teaching some new developers are getting, or indeed, if they are actually getting any.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900