Very often with Talend, it makes good sense to export and import files using a file-path being relative to the ROB-EX client folder. I.e. if your ROB-EX client folder in production is a network drive like \\myserver\robex\planner and you import and export from \\myserver\robex\planner\custom\talend, then you want to just specify “custom\talend” as the relative directory of your export and imports (and assume the job is started from the planner directory). This way your Talend job is not hardcoded to run from a named path but can be moved freely around between different ROB-EX installations.

Since Talend jobs prefer full file paths, we want, during runtime of the job, to construct a full path by concatenating our relative path to the current working directory. Read on to learn a way to do this.

Create a context variable

First create a Context variable called “EXPORT_ROOT_DIR”. In this case we provide a value for the context group “Default” (however the value will not be used). You can add other context groups like “Dev” or “QA”, to run your script during development. For production when running the “Default” context group, we want the job to auto build the full path based on the working directory it is started from.

Add a tJava component to your Talend job

The trick now is to change the value of the context variable “EXPORT_ROOT_DIR” dynamically when the jobs is executed. Add a single tJava component to your job. To make this component the first sub-job that runs, right click the tJava component and select “Trigger->On Subjob OK” and connect the green line to the first component of the actual sub-job you want to start.

Paste the following code into the tJava component. The code will, when the active context group is “Default”, change the “EXPORT_ROOT_DIR” context variable. Notice how we use the built in variable “contextStr” to lookup the active context group. Also notice how we use the System.getProperty(“user.dir”) command to lookup the current working directory and then add the relative path “/custom/talend”.

System.out.println("contextStr='" + contextStr + "'");
System.out.println("user.dir='" + System.getProperty("user.dir") + "'");
if ("Default".equalsIgnoreCase(contextStr)) {
  context.EXPORT_ROOT_DIR = System.getProperty("user.dir") + "/custom/talend/";
}
System.out.println("EXPORT_ROOT_DIR='" + context.EXPORT_ROOT_DIR + "'");

Using the relative path

When exporting to e.g. an XML file, then use the “context.EXPORT_ROOT_DIR” like shown below

Running the Talend Job

When you run the Talend job in production, make sure you place the job in “custom/talend/“. Also make sure to use the Talend setting “Window->Preferences->Talend->Import->Export->Add classpath jar in exported jobs” (see previous chapter)

Running using a .bat file

The bat file should looks like shown below. The highlightet markings are changes compared to the default generated .bat file. Notice that some lines are commented out with “REM”. We do not want to use the “cd” or “pushd”, because they would cause the working directory of the script to change (i.e. it would change the “user.dir” the startup directory)

REM %~d0
REM pushd %~dp0
java -Xms256M -Xmx1024M -cp "custom\talend\MasterMaterialAndInventory\classpath.jar" robextalend.mastermaterialandinventory_0_1.MasterMaterialAndInventory  --context=Default %* 
REM popd

The output from the “System.out.println” lines in our tJava component will during runtime looks like this:

INFO  gantt.utils.command.MacroManager - STDOUT>contextStr='Default'
INFO  gantt.utils.command.MacroManager - STDOUT>user.dir='C:\Program Files (x86)\ROB-EX\Planner'
INFO  gantt.utils.command.MacroManager - STDOUT>EXPORT_ROOT_DIR='C:\Program Files (x86)\ROB-EX\Planner/custom/talend/'

Running directly on java.exe without .bat file

As explained earlier, then running a Talend job from a .bat file is not always very practical and may not work on UNC network paths. So to achieve the same by directly running the job by java.exe, the RunExternalProgram macro should looks like this (also see previous chapter).

The output from the “System.out.println()” lines in our tJava component will during runtime looks like this (the same as when running the .bat script):

INFO  gantt.utils.command.MacroManager - STDOUT>contextStr='Default'
INFO  gantt.utils.command.MacroManager - STDOUT>user.dir='C:\Program Files (x86)\ROB-EX\Planner'
INFO  gantt.utils.command.MacroManager - STDOUT>EXPORT_ROOT_DIR='C:\Program Files (x86)\ROB-EX\Planner/custom/talend'

In case you run with an embedded Java, then just change java.exe in the “program” parameter to bin/jre/bin/java.exe

Feedback

Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.

Post your comment on this topic.

Post Comment