简体   繁体   中英

Java loading a resource from outside the classpath (pentaho)

I am a couple of weeks into Java programming and are trying to get the pentao report definition from a folder that is outside the class path, but have so far been unsuccessful.

The following successfully loads the definition from a folder contained in the class path, however I am unable to figure out how to get it from an absolute path outside the class path:

// Parse the report file
final URL reportDefinitionURL = classloader.getResource("some/path/inclass/Sample1.prpt");
final ResourceManager resourceManager = new ResourceManager();
final Resource directly = resourceManager.createDirectly(reportDefinitionURL, MasterReport.class);     
return (MasterReport) directly.getResource();

I am not sure if this is pentaho-specific or a generic Java issue, but how can I get the file definition based on a absolute path (linux) which is not located in the class path such as

"/usr/share/pentaho/Sample1.prpt" ?

I have tried this:

File file = new File("/usr/share/pentaho/");
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};                
ClassLoader cl = new URLClassLoader(urls);
final URL reportDefinitionURL = cl.getResource("Sample1.prpt");

I have also tried including the external path at runtime but that does not appear to work either:

java -cp ./lib/*:/usr/share/pentaho/*.prpt ...

Any assistance is greatly appreciated !

Default system class loader will look for resources only in the class path. If u know the absolute path, it would be easier to create the URL object directly:

URL reportDefinitionURL = new URL("file:/tmp/Sample1.prpt");

I am not familiar with the pentaho api's, so not sure how the following api works.

resourceMgr.createDirectly(reportDefinitionURL, MasterReport.class);     

In last line it is returning an instance of "MasterReport", so "MasterReport.class" should be there in your class path.

EDIT:

I just looked at this pentaho wiki - http://wiki.pentaho.com/display/Reporting/Using+the+DocumentBundle+Reader+and+Writer

Looks like the createDirectly() apis can take a file object

File reportDefinitionFile = new File ("/tmp/Sample1.prpt");
resManager.createDirectly(reportDefinitionFile, MyClass.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM