2009-10-18 7 views
2

Quand je tente d'initialiser le moteur de vitesse à l'aideApache Velocity ne peut pas Initialiser

VelocityEngine engine = new VelocityEngine(); 
engine.init(); 

je rencontre la même erreur lorsque je tente

Velocity.init(); 

org.apache.velocity.exception.VelocityException: Échec de l'initialisation d'une instance de org.apache.velocity.runtime.log.ServletLogChute avec la configuration d'exécution en cours.

Quelles peuvent causer cette exception?

Répondre

10

essayer quelque chose comme ceci:

Properties p = new Properties(); 
p.setProperty("resource.loader", "class"); 
p.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader"); 
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 

    try { 
     Velocity.init(p); 
    } catch(...., and handle excpetion 
    } 

vous pourrez désormais appeler:

VelocityContext vContext = new VelocityContext(context); 
//put things into vContext 
StringWriter sw = new StringWriter(); 
    try { 
     template.merge(vContext, sw); 

etc.

+5

ajoutant properties.setProperty \t \t ("runtime.log.logsystem.class" \t \t « org.apache.velocity. runtime.log.NullLogSystem "); a fait l'affaire –

+0

@Hamza: merci beaucoup! Ajout de ce paramètre à velocity.properties résolu le problème – Vanya

0

Le LogManager source suggère que l'exception d'origine est enveloppée dans la VelocityException. Cette exception enveloppée devrait vous donner plus d'informations.

Notez le commentaire associé dans le code.

/* If the above failed, that means either the user specified a 
* logging class that we can't find, there weren't the necessary 
* dependencies in the classpath for it, or there were the same 
* problems for the default loggers, log4j and Java1.4+. 
* Since we really don't know and we want to be sure the user knows 
* that something went wrong with the logging, let's fall back to the 
* surefire SystemLogChute. No panicking or failing to log!! 
*/ 
4

Le problème a été résolu en mettant le code ci-dessous mon application:

java.util.Properties p = new java.util.Properties(); 
p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); 
try { 
    Velocity.init(p); 
} catch (Exception e) { 
    System.out.println("FAIL!"); 
} 
+0

Cela fonctionne pour moi. Pourquoi 'org.apache.velocity.runtime.log.SystemLogChute' cause-t-il un échec? – BornToCode

0

Une manière propre:

java.util.Properties props = new java.util.Properties(); 
props.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());       
Velocity.init(props); 
0

utilisation

static { 
    /** Initialisation du moteur velocity */ 
    Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, 
      "org.apache.velocity.runtime.log.NullLogSystem"); 
    Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, 
      EjbConstants.VELOCITY_RESOURCE_LOADER_TYPE); 
    Velocity.setProperty(EjbConstants.VELOCITY_CLASSPATH_RESOURCE_LOADER, 
      ClasspathResourceLoader.class.getName()); 
} 

public static String createXMLFlux(final RemiseEffetBean bean, 
     final String maquetteId) { 

    try { 
     final VelocityContext context = new VelocityContext(); 
     final StringWriter swOut = new StringWriter(); 

     // Initialisation 
     Velocity.init(); 

     final Template template = Velocity.getTemplate(
       EjbConstants.XML_TEMPLATE_FILE, CharEncoding.UTF_8); 

     context.put(EjbConstants.VELOCITY_REMISE_EFFET, bean); 

     // id de la maquette pdf a generer 
     context.put(EjbConstants.VELOCITY_MAQUETTE_ID, maquetteId); 

     template.merge(context, swOut); 

     return swOut.toString(); 
    } catch (final ResourceNotFoundException e) { 
     LOGGER.error("La template n'a pas été trouvée", e); 
    } catch (final ParseErrorException e) { 
     LOGGER.error("Erreur du parsing de la template", e); 
    } catch (final MethodInvocationException e) { 
     LOGGER.error("Erreur lors de la substitution des données", e); 
    } catch (final Exception e) { 
     LOGGER.error("Erreur lors du traitement du fichier", e); 
    } 

    return null; 
} 
Questions connexes