ComposantsComponents

ComposantsComponents › Code personnaliséCustom Code

tLibraryLoad

tLibraryLoad

Code personnaliséCustom Code

Ajoute une bibliothèque Java (un jar) au job, pour que le code des tJava, tJavaRow, tJavaFlex et des routines puisse s'en servir. Il dit au Studio quel jar joindre au job ; sans bibliothèque dynamique, il ne fait rien d'autre pendant l'exécution. Adds a Java library (a jar) to the job, so that the code of tJava, tJavaRow, tJavaFlex and routines can use it. It tells the Studio which jar to add to the job; without a dynamic library, it does nothing else during the run.

EntréeInput

aucunenone il commence son sous-jobit starts its subjob

SortieOutput

aucunenone pas de fluxno flow

Déclencheurs en sortieOutput triggers

On Subjob Ok On Subjob Error On Component Ok On Component Error Run if

UtilisationUsage

OnSubjobOk tLibraryLoad_1 tJava_1

Placez-le seul en début de job, relié à la suite par On Subjob Ok. La bibliothèque vaut pour tout le job : un seul tLibraryLoad par jar suffit, quel que soit le nombre de composants qui s'en servent. Put it alone at the start of the job, connected to the rest with On Subjob Ok. The library applies to the whole job: one tLibraryLoad per jar is enough, whatever the number of components that use it.

  1. Choisissez le jar dans la liste Bibliothèque. Un jar absent de la liste s'installe d'abord dans les modules du Studio. Choose the jar in the Library list. A jar missing from the list is first installed in the Studio modules.
  2. Ajoutez ses import dans Paramètres avancés › Import du tLibraryLoad, ou dans ceux du composant qui l'utilise : ils valent pour tout le job. Add its import lines in Advanced settings › Import of the tLibraryLoad, or in those of the component that uses it: they apply to the whole job.
  3. Utilisez ses classes dans le code des autres composants. Le jar est joint au job pour la compilation, pour l'exécution, et dans le job construit. Use its classes in the code of the other components. The jar is added to the job for compilation, for the run, and in the built job.

ParamètresSettings

Le nom technique, en gris, est celui du paramètre dans le fichier du job. The technical name, in grey, is the parameter name in the job file.

ParamètreSetting DéfautDefault OngletTab Description
BibliothèqueLibraryLIBRARY — Paramètres simplesBasic settings Le jar à joindre au job. Le job ne garde que son nom (jsoup-1.17.2.jar) ; le jar lui-même est rangé dans le dépôt Maven du Studio, sous org.talend.libraries/nom/6.0.0-SNAPSHOT pour un jar installé à la main. The jar to add to the job. The job only keeps its name (jsoup-1.17.2.jar); the jar itself is stored in the Studio Maven repository, under org.talend.libraries/name/6.0.0-SNAPSHOT for a jar installed by hand.
Bibliothèques dynamiquesDynamics LibsHOTLIBS videempty Paramètres avancésAdvanced settings Chemins de jars chargés pendant l'exécution, une expression Java par ligne (plusieurs chemins dans une ligne : séparés par ; sous Windows, : sous Linux). Voir plus bas. Paths of jars loaded during the run, one Java expression per line (several paths in one line: separated by ; on Windows, : on Linux). See below.
ImportIMPORT //import java.util.List; Paramètres avancésAdvanced settings Lignes import complètes, placées en tête de la classe du job : elles valent pour tout son code. Complete import lines, placed at the top of the job class: they apply to all its code.

ExemplesExamples

Avec jsoup 1.17.2, une bibliothèque de lecture HTML sans dépendance. Sorties copiées depuis une exécution du job ; les messages de compilation sont abrégés. With jsoup 1.17.2, an HTML parsing library without dependencies. Outputs copied from a run of the job; compilation messages are shortened.

Avec le tLibraryLoadWith the tLibraryLoad

OnSubjobOk tLibraryLoad_1 tJava_1

Bibliothèque : jsoup-1.17.2.jar. Import : import org.jsoup.Jsoup;. Code du tJava : Library: jsoup-1.17.2.jar. Import: import org.jsoup.Jsoup;. tJava code:

System.out.println(Jsoup.parse("<p>Hello <b>Ada</b></p>").text());
Hello Ada

Sans lui : le job ne compile pasWithout it: the job does not compile

Le même tJava, avec l'import, mais sans tLibraryLoad dans le job. The same tJava, with the import, but without a tLibraryLoad in the job.

error: package org.jsoup does not exist
	import org.jsoup.Jsoup;
	                ^
error: cannot find symbol
System.out.println(Jsoup.parse("<p>Hello <b>Ada</b></p>").text());
                   ^
  symbol:   variable Jsoup

Bibliothèques dynamiquesDynamic libraries

Les jars de Bibliothèques dynamiques ne sont pas joints au job : ils sont lus sur le disque au moment de l'exécution, depuis un chemin qui peut venir d'une variable de contexte. Le tLibraryLoad les place dans un nouveau chargeur de classes, qu'il donne au fil d'exécution (Thread.currentThread().getContextClassLoader()). The jars of Dynamics Libs are not added to the job: they are read from the disk at run time, from a path that can come from a context variable. The tLibraryLoad puts them in a new class loader, which it gives to the running thread (Thread.currentThread().getContextClassLoader()).

Utiliser un jar chargé à l'exécutionUse a jar loaded at run time

Bibliothèques dynamiques : "C:/libs/jsoup-1.17.2.jar" (dans l'essai, le chemin du jar sur le poste). Bibliothèque vide. Le compilateur ne connaît pas ses classes : le tJava les atteint par réflexion. Dynamics Libs: "C:/libs/jsoup-1.17.2.jar" (in the test, the path of the jar on the machine). Library empty. The compiler does not know its classes: the tJava reaches them through reflection.

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class<?> jsoup = Class.forName("org.jsoup.Jsoup", true, loader);
Object doc = jsoup.getMethod("parse", String.class).invoke(null, "<p>Hello <b>Grace</b></p>");
System.out.println(doc.getClass().getMethod("text").invoke(doc));
Hello Grace

C'est utile pour une bibliothèque qui change selon l'environnement, ou qu'un autre outil cherche dans le chargeur de classes du fil d'exécution. Pour un jar dont le code se sert directement, prenez Bibliothèque. It is useful for a library that changes with the environment, or that another tool looks up in the class loader of the running thread. For a jar that the code uses directly, take Library.

ConseilsTips

Dépendances du jarJar dependencies

Le tLibraryLoad joint un seul jar, sans les bibliothèques dont il dépend. Pour un jar qui en a, ajoutez un tLibraryLoad par dépendance, sinon l'exécution s'arrête sur une NoClassDefFoundError. The tLibraryLoad adds a single jar, without the libraries it depends on. For a jar that has some, add one tLibraryLoad per dependency, otherwise the run stops on a NoClassDefFoundError.

Pour une routineFor a routine

Une routine du projet qui utilise un jar peut aussi le déclarer elle-même, dans ses bibliothèques (clic droit sur la routine › Modifier les bibliothèques de routines). Le tLibraryLoad sert surtout au code écrit dans les composants du job. A project routine that uses a jar can also declare it itself, in its libraries (right-click on the routine › Edit Routine Libraries). The tLibraryLoad is mostly for code written in the job components.

Voir aussiSee also