We hope you learn how to use TotalCross Runtime and Process implementations. See more about:
Java 7 Runtime class​
Java 8 Process class​
These implementations work only for Linux platforms
Can compile C ++ applications and finish Get Started:
Use external codes with Totalcross:
Step 1: create a blank project based in HelloWorld
of VS Code plugin (we named it RunningCpp
)
Step 2: create an I/O sample in C++, in our case we did:
#include <iostream>#include <string>​int main(){std::string input;std::getline(std::cin, input);std::cout << "\nI received: " + input + "\n";return 0;}
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 resultsLabel label;label = new Label();label.setBackForeColors(Color.WHITE, Color.BLACK);
Step 5: create a child process:
// Process initializationProcess process = null;
Step 6: output to the target program:
// Output to programbyte[] output = "Take the output!!!\n".getBytes(); // convert string to// byte arraytry {process = Runtime.getRuntime().exec("./io"); // execute your// application (sh like)process.getOutputStream().write(output, 0, output.length); // write output into// output stremprocess.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 programString input;try {// Read line by line the buffered streamLineReader lineReader = new LineReader(Stream.asStream(process.getInputStream()));while ((input = lineReader.readLine()) != null) {label.setText(input);}} catch (IOException ioe) {ioe.printStackTrace();};​// Add label to windowadd(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 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);}@Overridepublic void initUI() {// Label to show the resultsLabel label;label = new Label();label.setBackForeColors(Color.WHITE, Color.BLACK);// Process initializationProcess process = null;// Output to programbyte[] output = "Take the output!!!\n".getBytes(); // convert string to// byte arraytry {process = Runtime.getRuntime().exec("./io"); // execute your// application (sh like)process.getOutputStream().write(output, 0, output.length); // write output into// output stremprocess.waitFor(); // blocking method// (wait io finish)} catch (IOException ioe) {ioe.printStackTrace();} catch (InterruptedException ie) {ie.printStackTrace();};// Input from programString 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 windowadd(label, CENTER, CENTER);}}
Java 7 Runtime class​
Java 8 Process class​