Detecting the operating system at run time and dynamically setting the file path - 7.3

Java custom code

Version
7.3
Language
English (United States)
Product
Talend Big Data
Talend Big Data Platform
Talend Cloud
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for Big Data
Talend Open Studio for Data Integration
Talend Open Studio for Data Quality
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
Module
Talend Studio
Content
Data Governance > Third-party systems > Custom code components (Integration) > Java custom code components
Data Quality and Preparation > Third-party systems > Custom code components (Integration) > Java custom code components
Design and Development > Third-party systems > Custom code components (Integration) > Java custom code components

To allow the Job to run on different operating systems, you can set the file path using a context variable as in the following example Job.

This Job is composed of:

  • a tJava component to initialize the context variable and detect the operating system,
  • a tFixedFlowInput component to generate data,
  • a tFileOutputDelimited component to generate a .txt file.

Procedure

  1. Create a new Job named DetectOSTypeDemo and place a tJava component from the Palette on the workspace.
  2. Place a tFixedFlowInput and a tFileOutputDelimited on the workspace and connect them with a Row Main link. Connect the tJava to the tFixeFlowInput component with a Trigger > On Subjob Ok link.
  3. Click the tJava component to open its Basic setttings view, add the following code to the Code area to initialize the context variable and to detect the operating system:
    String os = System.getProperty("os.name").toLowerCase();  
    if(os.indexOf("win") >= 0){
    context.filePath="C:/temp/out.txt";
    }else { //Unix / Linux file path
    context.filePath="/var/temp/out.txt";
    }
  4. Click the tFixedFlowInput component to open its Basic setttings view.
  5. Click the Edit schema button and add a new column named name. In the Values box, add the value "Hello world" to generate output data as below:
  6. In the Contexts view of the component, create a context variable (named filePath of String type) for setting the path of output file, as shown in the following figure.
  7. Click the component view of tFixedFlowInput and set the file name with the following context variable context.filePath in the File Name field.
  8. Execute the Job. If the Job is executed on a Windows system, the data is written to the "C:/temp/out.txt" file. If the Job is executed on a Unix or Linux system, the data is written to the "/var/temp/out.txt" file.