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 :

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 :

#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

Recommended Answers

All 2 Replies

So the c++ code is already compiled as an exe program? Then in the java program construct a string that contains the name of the program followed by the command-line arguments you want to pass. After that call a function that executes other programs. I'm not a java programmer but in C/C++ it would be system().

Maybe this tutorial will help you

Yes the c++ compiled as an exe, and it suppose to take parameter that has been send by the java code
Thank you i will try this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.