0

This is my maven web project structure, and I'm using java 8 + netbeans + jboss eap 6.

Project
|__/src
  |__/main
    |__/java
    |__/resources
    | |__/velocity
    |     -template.vm
    |__/webapp

Once application is running I'm trying get de resource path /proyect/src/main/resources/velocity/ this way:

String path = SomeClass.class.getResource("/" + templateName).getPath();

but instead I'm getting a path inside jboss EAP /bin folder like this:

C:/Users/MyUser/EAP-6.4.0/bin/content/MyWebApp.war/WEB-INF/classes/velocity/

And obviously that path doesn't exist.

Steve C
  • 18,876
  • 5
  • 34
  • 37
bit.mind
  • 23
  • 1
  • 6

1 Answers1

0

As it seems you're using the Apache Velocity Engine you need to configure it to use a ClasspathResourceLoader.

Then you can simply use the path /velocity/template.vm for template processing.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • Yes, I'm doing it that way. I'm setting that path into velocity's property `VelocityEngine.FILE_RESOURCE_LOADER_PATH`. – bit.mind May 15 '17 at 16:23
  • You need to load those files as **classpath** resources, not **file** resources because they exist inside the war file and may not be accessible directly in the file system. – Steve C May 15 '17 at 23:56
  • I suppose the instruction `SomeClass.class.getResource()` lookup into classpath, right? – bit.mind May 17 '17 at 05:47
  • Yes, but that particular incarnation will yield a resource relative to the package containing `SomeClass`. You should check the answer to http://stackoverflow.com/questions/3861989/preferred-way-of-loading-resources-in-java for a thorough understanding – Steve C May 17 '17 at 05:59