Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With Tomcat8, loading of resources located in jar fails #167

Closed
svanbegin opened this issue Oct 30, 2016 · 3 comments
Closed

With Tomcat8, loading of resources located in jar fails #167

svanbegin opened this issue Oct 30, 2016 · 3 comments
Labels

Comments

@svanbegin
Copy link

svanbegin commented Oct 30, 2016

When upgrading a webapp based on StringTemplate4 from Tomcat7 to Tomcat8,
we were faced with a resource loading issue. The resources being located in a jar within a war.

Resource management module has been rewritten in Tomcat8 and as a side effect in StringTemplate, the root path now ends with a forward slash:

STGroupDir(st/tabs) found via CLASSPATH at jar:file:/opt/tomcat8/webapps/app/WEB-INF/lib/ui-impl.jar!/st/tabs/

StringTemplate now tries to find a resource with a corrupted (double slash) path:

jar:file:/opt/tomcat8/webapps/app/WEB-INF/lib/ui-impl.jar!/st/tabs//tabs_js.st doesn't exist

I created a derived class which is just a temporary workaround but illustrates how I solved this issue:

public class STGroupDirPatched extends STGroupDir {

public STGroupDirPatched(String dirName) {
    super(dirName);
    if (root != null && root.toString().endsWith("/")) {
        try {
            String url = root.toString();
            root = new URL(url.substring(0, url.length() - 1));
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

}
@thomasKalmar
Copy link

Isn't the Problem solvable by replacing usage of for example
f = new URL(root+prefix+unqualifiedFileName);
with
f = new URL(root, prefix+unqualifiedFileName);
see http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#URL(java.net.URL,%20java.lang.String) for details. This should take care of the trailing /

@solussd
Copy link

solussd commented May 2, 2018

This PR: #199 combined with constructing a STGroupFile using a url from a Classloader's getResource method should solve these kinds of issues.

Constructing paths for particular environments is likely to break in others (e.g., code packaged in a jar vs exploded at a directory).

@parrt
Copy link
Member

parrt commented Nov 8, 2018

I believe this was resolved with #207

@parrt parrt closed this as completed Nov 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants