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
-
Create a new Job named DetectOSTypeDemo and place a
tJava component from the Palette on
the workspace.
-
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.
-
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";
}
-
Click the tFixedFlowInput component to open its
Basic setttings view.
-
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:
-
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.
-
Click the component view of tFixedFlowInput and set the file
name with the following context variable context.filePath in
the File Name field.
-
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.