Skip to main content Skip to complementary content

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

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 Context 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.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!