How to access a Java variable?

One Star

How to access a Java variable?

Hi,
I've got a tJava and set a variable String var1 = "test";
Now I want to access this variable somewhere else, for example in a MsgBox or what else.
How can I access it?
globalMap.get("tJava_1_...");
There is only ERROR_MESSAGE .. _1_currentPath doesn't work either.
Can someone help me?
Thank you.
One Star

Re: How to access a Java variable?

You need to use the globalMap map variable or create a context variable and use that. The scope of the tJava components is limited. You need a global scoped variable.
Community Manager

Re: How to access a Java variable?

Hello
Frist, you need to set it to a global var. eg:
String var1="test";
globalMap.put("varName",var1);
then, use this var like this:
(String)globalMap.get("varName")
Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: How to access a Java variable?

Hi,
it works within one job, but when I call tRunJob and try to System.out.println the variable in the childjob I see: null
For the tRunJob I've choose to: Transmit whole content.
What do I have to do if I want to pass this variable to a child?
Bye, Chris
One Star

Re: How to access a Java variable?

The globalMap instance is specific to a job, transmitting the context to a child job wont changes this.
If you intent to transmit a variable to child job, use the context then, it should be straitforward.
Employee

Re: How to access a Java variable?

Hello,
Existing two way to manage variable and parameter in a talend job.
1) globalMap is used to set variable in a Job, it's a hashmap
2) context var is used to set parameters
So, to achieve your goal,
1) you need to create context var in your childjob
2) With the tRunJob, use the context param tab, create a row, select the childjob context var in the first column, and set your expresion in the second one : globalMap.get("MyVar")
the transmit whole context checkbox is only for to transmit value of context var with same name from fatherjob to childjob.
One Star

Re: How to access a Java variable?

Eventually I got it .. I tried so hard to set a context in my fatherjob, I didn't get the idea to setup a context in the childjob! Smiley Sad
Thank you for your help! Smiley Happy
One Star

Re: How to access a Java variable?

Hi,
How Can I share the global map between father an son job. I see that it's possible using context. In fact I have lots of parameters , so It will be hard to create a context variable for each parameter.
Thanks for your replay
James