One Star

creation component - import java libraries

Hi,
I create a new java component, which use java libraries. So i add at beginning of my script
<%@ jet
imports="
org.talend.core.model.process.INode
org.talend.designer.codegen.config.CodeGeneratorArgument
org.talend.core.model.process.ElementParameterParser
java.awt.Image
javax.imageio.ImageIO
"
%>
But then i trie to use this component in mjob , the generator doesn't add lines in script. Why ?
18 REPLIES
One Star

Re: creation component - import java libraries

G'day,
I have the same problem (I think): I created a component of my own that uses different external libraries and when I try to execute my job with this component, I have a "My_Class cannot be resolved to a type".
The only workaround I know is to use tLibraryLoad to load my libraries one after the other but, when I have many different libraries, it soon becomes quite chaotic in the "Job Design View" screen as there must be one component by library.
My question is: Is it possible to load the content of a folder in the CLASSPATH instead of having to select one JAR at a time?
I have tried to put my jar in the same folder as my component (like I saw for some other components such as 'tJasperOutput' for example) but it doesn't seem to be working. What am I missing there?
Thanks again for your help and keep on the good work guys!
Chris
One Star

Re: creation component - import java libraries

you have to modify the xml file corresponding to your component.
for my chart components I use jfreechart and jcommons so I've put this line to my xml :
<CODEGENERATION>
<IMPORTS>
<IMPORT NAME="JFreeChart" MODULE="jfreechart-1.0.3.jar" REQUIRED="true"/>
<IMPORT NAME="JCommon" MODULE="jcommon-1.0.6.jar" REQUIRED="true"/>
</IMPORTS>
</CODEGENERATION>
I hope it will help you
olivier
One Star

Re: creation component - import java libraries

Super cool !!!
Cheerios !
Chris
One Star

Re: creation component - import java libraries

you'll welcome Smiley Wink
Seventeen Stars

Re: creation component - import java libraries

hi,
doing my first Component , i've got similar problem !
I've added element IMPORT in configuration XML file :
<CODEGENERATION>
<IMPORTS>
<IMPORT NAME="jcifs" MODULE="jcifs-1.3.0.jar" REQUIRED="true"/>
</IMPORTS>
</CODEGENERATION>

and also modified string imports of begin javajet :
<%@ jet
imports="
org.talend.core.model.process.INode
org.talend.core.model.process.ElementParameterParser
org.talend.core.model.metadata.IMetadataTable
org.talend.core.model.metadata.IMetadataColumn
org.talend.core.model.process.IConnection
org.talend.core.model.process.IConnectionCategory
org.talend.designer.codegen.config.CodeGeneratorArgument
org.talend.core.model.metadata.types.JavaTypesManager
org.talend.core.model.metadata.types.JavaType
java.util.List
java.util.Map
jcifs.smb.NtlmPasswordAuthentication
jcifs.smb.SmbException
jcifs.smb.SmbFile
jcifs.smb.SmbFileInputStream
jcifs.smb.SmbFileOutputStream
"
%>

without "jcifs import" it works (no compilation problem) ... but when I add these ones , I've a "Component compile fail".
I've tried to add jcifs jar file in Talend lib repository, and also in my componenent repository ...!
thanks for your help
laurent
Employee

Re: creation component - import java libraries

Hello kzone,
The import in the scriptlet part (<% %>) are only used when generating the component templates.
You can't do import for code executed at runtime of your job.
In your component javajet remove the import declaration and then use the fully qualified class names
for example :
jcifs.smb.SmbFileInputStream sis = new jcifs.smb.SmbFileInputStream(...);

HTH,
Seventeen Stars

Re: creation component - import java libraries

hi mhirt,
thanks for your explanation and precisions ....
hope now i 'll be able to finish it , and put it in the ecosystem !
++
One Star

Re: creation component - import java libraries

You can't do import for code executed at runtime of your job.

Is this going to be changed in future versions? or is this a design desision?
Regards,
Bas van Reeuwijk.
Employee

Re: creation component - import java libraries

It's a design choice : if component A and component B use 2 Array class in 2 different packages, they can not be used in the same Job if we add an import feature.
Fully qualified name is the only solution to avoid such problems.
Best Regards,
One Star

Re: creation component - import java libraries

Ok, I understand.
Thanks for the quick reply.
Bas.
One Star

Re: creation component - import java libraries

Is the jar-files must be in the component directory only?
Can I put its in <Talend_home>/lib/java and use its the same way?
Employee

Re: creation component - import java libraries

Is the jar-files must be in the component directory only?
Can I put its in <Talend_home>/lib/java and use its the same way?

For private usage yes. But if commit to ecosystem, had better include related lib in component folder, unless they have already been used in other component. Notice that if you look into the fresh TOS zip package, the lib folder is not exist at the very beginning, all the lib in %TALEND_HOME%/lib/ are gethered after the studio start.
One Star

Re: creation component - import java libraries

Now I used my jar's for code executed at runtime of my job.All worked correctly.
But the imports in the scriptlet part (<% %>) to use when generating the component templates, don't work.
Can we make some import declaration in component javajet?
For example, like this:
<%@ jet
imports="
org.talend.core.model.process.INode
org.talend.core.model.metadata.IMetadataTable
org.talend.core.model.metadata.IMetadataColumn
...
org.myproject.myclasses.MyClass
"
%>
I need to use later:
<% MyClass myclass = new MyClass(); %>
How can I do something like this?
Now there is an exception (similar #6 of this list)
BR,
Roman
One Star

Re: creation component - import java libraries

I want to use classes of my plug-in project when generating the component templates. If I import my class in javajet file, i am getting compilation issue due to the package invisibilty.
In many javajet files I found packages imported only from org.talend.core and org.talend.designer.codegen projects. Is there any limitation to use only these projects as dependencies for generating the component templates?
Regards,
Vijay
One Star

Re: creation component - import java libraries

Hi,
I reply to this old post because I have the same problems. I am developing a new Talend component which uses XMLRPC to connect to a server. I need many imports, some from XMLRPC libraries. I have made like it's written:
<CODEGENERATION>
<IMPORTS>
<IMPORT NAME="openerp" MODULE="openerp.jar" REQUIRED="true"/>
</IMPORTS>
</CODEGENERATION>
openerp.org.apache.xmlrpc.client.XmlRpcClient client = new openerp.org.apache.xmlrpc.client.XmlRpcClient();
At the runtime, an error appears like "openerp is not recognized as a valid type". What must i do ? This is wrong ?
Thank for help, kind regards.
Zak
Employee

Re: creation component - import java libraries

Hi
openerp.org.apache.xmlrpc.client.XmlRpcClient client = new openerp.org.apache.xmlrpc.client.XmlRpcClient();

Are you sure that this is the correct package name? I see references on the web to org.apache.xmlrpc.client.XmlRpcClient, but I see none to openerp.org.apache.xmlrpc.client.XmlRpcClient.
You only need to declare the library - it's not necessary to prefix its name to the classes it provides. Try
org.apache.xmlrpc.client.XmlRpcClient client = new org.apache.xmlrpc.client.XmlRpcClient();

Imad
One Star

Re: creation component - import java libraries

Hi Imad,
Thank you for your quick answer but, I'm sure Smiley Frustrated Because of integrity problems of the officiel XmlRpc library, I have made mine.
I have tried with your version but now I have a "java.lang.NoClassDefFoundError".
One Star

Re: creation component - import java libraries

Hello, I often had an issue with jars in custom components.
Check if the jar is copied into the lib/java directory under your install path.
It should, but it happened to me that the file was not automatically copied from the component folder to there when installing the component.
Particularly if a previous version of the file (with the same name) was already there, it was not copied.
If this is the case, the workaround is to copy manually the jar.
good luck