One Star

Question about custom Java code

I'm very new to Talend so this may be the most basic question ever!
I have java code in the following format that I'd like to use in a tJava component:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class XYZ {
public static void main(String[] args) throws Exception {

// code here
}
private static String loadStream(InputStream s) throws Exception {
// code here
}
}
In playing with the tJava component, I see I can put my import statements in the Advanced Settings tab. However, this component only seems to allow simple Java code snippet (no class declaration or private static String loadStream....).
I then read about routines and thought maybe I could put my class definition in a routine and then basically put the code contained within main in the tJava component. However, my class definition depends on the import libraries and I could only see how to import external libraries through a JAR file for routines.
I hope I missing something obvious but could anybody help me understand how to bring a java program in the format I'm using into Talend?
Thanks so much!
6 REPLIES
Seventeen Stars

Re: Question about custom Java code

Yes routines which depends on external libraries needs an explicit tLibraryLoad component in every job which uses this routine. Thats why I would not build such routines and would instead create a custom component which can carry a couple of jars.
You can create your own class (also with inheritance) in the code section. Simply create a routine and code instead of static method your own class. This class will be available in other routines (static class methods).
The better way to start coding Java in a job is using the tJavaFlex component because you can create a instance of a utilities bean at the start of the sub job and uses the bean within a flow and do anything at the end of the flow with it.
This is also one good way to create a first implementation of an idea for an component because you can create for all 3 parts of an component code without dealing with the javajet code.
One Star

Re: Question about custom Java code

Thank you for your response but is it possible for you to give me a little more direction? It's been a while since I've worked much with java.
I'm still confused because the only place I can find to specify the import java.io... statements is in the Advanced Settings of tJava. But tJava doesn't seem to allow me to put a full class definition in there??
When creating a routine, I seem to have to specify a specific library file location, not just java.io... library name??
Seventeen Stars

Re: Question about custom Java code

This code will be added into a method. The import statements does not work in a method. Thats because you need to add them in a separate section or if you want avoid trouble with it use full qualified class names.
Now you probably can imagine nothing of your code is suitable for the tJava component.
If you want get an idea write in the tJava some very simple code lines like
String test = "I am here";

and take a look at the code view of the job. This teaches you everything what you need to write proper Java code for tJava component.
One Star

Re: Question about custom Java code

Thanks so much for your help! I now have this working the way that I need it to. Smiley Happy
One Star

Re: Question about custom Java code

Hi i want help form u...
Actually i have created the custom component but i stucked in adding Routine inside the custom component.
Please help me with this 
Thanks in advance
One Star

Re: Question about custom Java code

Thank you for advise!