LogoLogo
6.1.1
6.1.1
  • TotalCross Overview
  • TotalCross Javadoc
  • TotalCross Changelog
  • Roadmap
  • Documentation
    • Getting Started
      • First embedded project with TotalCross
    • Components
      • Accordion
      • Aligned Labels
      • Button
      • Check
      • ComboBox
      • Dynamic Scroll
      • Edit
      • Floating Button
      • Gpiod
      • Grid
      • GridContainer
      • Image
      • ImageControl
      • ImageList
      • Label
      • Material Icons
      • Material Window
      • MessageBox
      • Multi Edit
      • Progress Bar
      • Progress Box
      • Radio
      • Radio Group
      • Scroll Container
      • Side Menu
      • Slider
      • Sliding Window
      • Spin List
      • Spinner
      • Switch
      • Tabbed Container
      • Velocimeter
    • APIs
      • API Overview
      • API Rest
      • Asynchronous Task
      • Camera
      • Control
        • Main Window
        • Window
        • Container
      • GPS
      • HTTPS and SSL
      • JSON
      • Maps
        • Maps - Deprecated
        • Static Map
      • Material Design Standards
      • Ninepath
      • Notifications
      • PrinterManager
      • Push Notification Firebase
      • Scanner
      • SOAP
      • Socket
      • SocketServer
      • SQLite Encryption
      • QR Code Generator
      • totalcross.sys
      • Youtube API
    • Creating an Issue
    • Contributing
      • Branch workflow
      • Writing documentation
    • Guides
      • App Architecture
        • Suggested Architecture
        • Why do Design Patterns help with the application's organization?
          • MVC Architecture Pattern
          • Template Pattern
          • Data Persistence: DAO Pattern.
        • Separation of concepts: What is the best way to create UI interfaces?
        • Positioning
          • Manual Positioning
        • Relative Positioning
        • Best practices to improve project maintenance
      • Device Simulator
      • Package your app from scratch
        • TotalCross SDK
        • Environment Variables in IDE
          • Eclipse
          • IntelliJ
        • Deploy your app with a dependecy TC
        • Deploy iOS
          • Using Development certificate to test your apps
      • Understanding TotalCross for Linux ARM
      • Running C++ applications with TotalCross
      • Web Services
    • Miscelaneous
      • Java JDK 8
      • Maven
      • Installing Visual Studio Code
    • FAQ
      • IMEI in Android 10
Powered by GitBook
On this page
  • Introduction
  • Requirements
  • Guide
  • See more
  • References

Was this helpful?

  1. Documentation
  2. Guides

Running C++ applications with TotalCross

PreviousUnderstanding TotalCross for Linux ARMNextWeb Services

Last updated 5 years ago

Was this helpful?

Introduction

We hope you learn how to use TotalCross Runtime and Process implementations. See more about:

  • Java 7

  • Java 8

These implementations work only for Linux platforms

Requirements

Can compile C ++ applications and finish Get Started:

Guide

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 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);
    }
}

References

Java 7

Java 8

Runtime class
Process class
Running C++ applications with TotalCross
Runtime class
Process class