Support #5864


Using XSLT packages in a jar file

Added by Mark Dunn about 2 months ago. Updated about 2 months ago.

Start date:
Due date:
% Done:


Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:


For the project I am working on my entire code needs to be bundled in a jar file.

The Saxon configuration file is in the resources folder in the jar. (See attached screenshot jar-resources.gif.)

The packages are in a subfolder "packages".

The config file references the packages with a relative path, e.g.

<package name="" version="1.0" sourceLocation="packages/logger.xsl"/>

When I run the application I get an error saying that the package could not be located:

I/O error reported by XML parser processing file:/C:/SVN/software/Atlas/BITS2MARC/MARC_creator/packages/logger.xsl: C:\SVN\software\Atlas\BITS2MARC\MARC_creator\packages\logger.xsl (The system cannot find the path specified)

The application is looking outside the jar for the packages even though the config file is being read from within the jar.

Is there a way of writing the element so that the packages are picked up from the resources folder in the jar?


jar-resources.gif (10.6 KB) jar-resources.gif Mark Dunn, 2023-01-31 13:56
Actions #1

Updated by Michael Kay about 2 months ago

How do you supply details of the configuration file to Saxon? The first thing to establish is that Saxon knows the base URI.

Although JAR file URIs are non-standard, if we have a base URI that's a file within a JAR, we should be able in principle to use it to locate other files in the same JAR using relative URIs; the most likely explanation for the observed effect is that we don't know the base URI.

Actions #2

Updated by Mark Dunn about 2 months ago

I'm getting the file from the resources folder as a stream:

InputStream saxonConfig = getClass().getClassLoader().getResourceAsStream("config-saxon.xml");
this.saxon = new Processor(new StreamSource(saxonConfig));

Is the base URI getting lost in this process?

I'm wondering if I might have better results by supplying an instance of the Configuration class instead of the file. But I've not looked into that.

Actions #3

Updated by Michael Kay about 2 months ago

Well, there's no systemId property on the StreamSource, so Saxon doesn't have a base URI to work with.

And it's not easy to suggest what you should supply. You could try "classpath:config-saxon.xml" - I've no idea if that would work.

Actions #4

Updated by Mark Dunn about 2 months ago

Perfect, thanks!

A method that works:

ClassLoader cl = getClass().getClassLoader();
String systemID = "config-saxon.xml";
InputStream saxonConfig = cl.getResourceAsStream(systemID);
URL url = cl.getResource(systemID);
Source source = new StreamSource(saxonConfig);
this.saxon = new Processor(source);
Actions #5

Updated by Michael Kay about 2 months ago

Great, good to know.

As a matter of interest, what does the result of url.toExternalForm() look like?

Actions #6

Updated by Mark Dunn about 2 months ago

When I run the application within NetBeans the value is


When I run the application as a jar the value is


(Maven has placed the config-saxon.xml file at the top level of the jar when building the jar.)

Please register to edit this issue

Also available in: Atom PDF