Untitled

 avatar
unknown
plain_text
a month ago
2.4 kB
2
Indexable
private static StringBuilder ProcessingCPP(Map<String, Object> data_to_cpp) {

        Gson gson = new Gson();

        String json = gson.toJson(data_to_cpp);

        logField1stTab.append("\n\n\n\n");
        logField1stTab.append(json);

        System.out.println(json);
        StringBuilder jsonBuilder = new StringBuilder();

        try {
            String workingDirectory = System.getProperty("user.dir");


            String cppSourcePath = "C:/Users/T14/OneDrive/Pulpit/Inzynierka";
            String compileCommand = "g++ -o algorytm.exe";

            Process compileProcess = Runtime.getRuntime().exec(compileCommand);

            int compileExitCode = compileProcess.waitFor();
            if (compileExitCode == 0) {
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream()));
                String errorLine;
                while ((errorLine = errorReader.readLine()) != null) {
                    System.err.println(errorLine);
                }
            }
            System.out.println("C++ compiled successfully");

            String cppProgramPath = "C:/Users/T14/OneDrive/Pulpit/Inzynierka/algorytm.exe";
            //String cppProgramPath = workingDirectory + File.separator + "Holmes" + File.separator + "scripts" + File.separator + "RG_TPN" + File.separator + "RG_TPN.exe";
            //System.out.println("C++ program path: " + cppProgramPath);
            //System.out.println(cppProgramPath);

            ProcessBuilder processBuilder = new ProcessBuilder(cppProgramPath);
            Process process = processBuilder.start();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
            writer.write(json);
            writer.newLine();
            writer.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String response;

            System.out.println("Response from C++ programm:");
            while ((response = reader.readLine()) != null) {
                System.out.println(response);
                jsonBuilder.append(response);
            }
            process.waitFor();


        }catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }


        return jsonBuilder;

    }
Leave a Comment