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

Overview

You often need to know the current operating system when running a Talend Job. Talend Studio supports cross-platform Job execution, and allows you to develop Jobs on one machine and run them on other machines. Some components, such as File components, always require a local file path. Operating systems do not all have the same format for paths in the file system.

 

To make the Job run on different operating systems, the component needs to detect the current operating system type and set a dynamic file path at run time.


Environment

This procedure was written with:

  • Talend Open Studio for DI 5.0-r72978
  • JDK version: Sun JDK build 1.6.0_26-b03
  • Operating system: Windows XP SP3

Talend verified this procedure to be compatible with:

  • Data Integration releases: 4.2.3, 4.2.4, 5.0.0, 5.0.1, 5.0.2, 5.1.1, 5.1.2, 5.2.0

Procedure

To allow the Job run on different operating systems, you can set the file path using a context variable. The context variable gets initialized at the beginning of the Job. For example, you can detect the operating system and initialize the context variable in the tJava component as follows:

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

To help clarify this example, below is an example Job named DetectOSTypeDemo. The detailed Job settings can be seen below:

1_012.png


2_010.png


3_006.png


4_006.png


Execute the Job. If the Job is executed on a Windows system, 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.

Version history
Revision #:
3 of 3
Last update:
‎06-19-2017 11:15 PM
Updated by:
 
Labels (1)
Tags (1)