It's a simple application to get an I/O input and shortly thereafter return it as output.
Step 3: compile (something like this):
Step 4: inside initUI method at RunningCpp class, create a label to show the results:
// Label to show the results
Label label;
label = new Label();
label.setBackForeColors(Color.WHITE, Color.BLACK);
Step 5: create a child process:
// Process initialization
Process process = null;
Step 6: output to the target program:
// Output to program
byte[] output = "Take the output!!!\n".getBytes(); // convert string to
// byte array
try {
process = Runtime.getRuntime().exec("./io"); // execute your
// application (sh like)
process.getOutputStream().write(output, 0, output.length); // write output into
// output strem
process.waitFor(); // blocking method
// (wait io finish)
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
};
Step 7: read the C++ program output as input to TotalCross application
// Input from program
String input;
try {
// Read line by line the buffered stream
LineReader lineReader = new LineReader(Stream.asStream(process.getInputStream()));
while ((input = lineReader.readLine()) != null) {
label.setText(input);
}
} catch (IOException ioe) {
ioe.printStackTrace();
};
// Add label to window
add(label, CENTER, CENTER);
Step 8: run TotalCross: Package with VS Code plugin or run mvn package in your terminal.
Step 9: copy C++ binary to target folder (something like):
Step 10: run your program!!!
See more
See our article about how to run RS232 protocol. See the full code:
package com.totalcross;
import java.io.IOException;
import java.lang.Process;
import totalcross.ui.Label;
import totalcross.ui.MainWindow;
import totalcross.ui.gfx.Color;
import totalcross.io.LineReader;
import totalcross.io.Stream;
import totalcross.sys.Settings;
public class RunningCPP extends MainWindow {
public RunningCPP() {
setUIStyle(Settings.MATERIAL_UI);
}
@Override
public void initUI() {
// Label to show the results
Label label;
label = new Label();
label.setBackForeColors(Color.WHITE, Color.BLACK);
// Process initialization
Process process = null;
// Output to program
byte[] output = "Take the output!!!\n".getBytes(); // convert string to
// byte array
try {
process = Runtime.getRuntime().exec("./io"); // execute your
// application (sh like)
process.getOutputStream().write(output, 0, output.length); // write output into
// output strem
process.waitFor(); // blocking method
// (wait io finish)
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
};
// Input from program
String input;
try {
LineReader lineReader = new LineReader(Stream.asStream(process.getInputStream()));
while ((input = lineReader.readLine()) != null) {
label.setText(input);
}
} catch (IOException ioe) {
ioe.printStackTrace();
};
// Add label to window
add(label, CENTER, CENTER);
}
}