0

I have an xls spreadsheet that I'm querying using metaModel, and I want to keep the xls file in the jar, because it won't be updated. The method used for creating the data context doesn't allow inputstreams, I tried using this code:

DataContext dataContext = DataContextFactory.createExcelDataContext(getClass().getResourceAsStream("database.xls"));

Unfortunately this doesn't work, as the method createExcelDataContext doesn't take inputstreams as a parameter. Is there any way to keep the file in the jar?

It seems the easiest way to do this is just export to csv, as this is easily done.

Mukhi
  • 143
  • 12
  • 1
    What does the DataContext allow? Which DataContext API are you using? – anders.norgaard Jun 24 '12 at 19:04
  • 1
    Also, what IDE and compiler are you using? – gobernador Jun 24 '12 at 19:04
  • @anders.norgaard It only allows a file object, and I just downloaded and installed from here: http://metamodel.eobjects.org/ – Mukhi Jun 24 '12 at 19:07
  • @gobernador I'm using eclipse – Mukhi Jun 24 '12 at 19:07
  • 1
    @Mukhi one thing that I've done is create a `/res` subpackage and keep all my non-java files in there. I think you can just navigate to the package location with Windows Explorer and paste the excel document in there. It's worked for me in the past. – gobernador Jun 24 '12 at 19:10
  • @gobernador I'll try, but will this allow others to access it if I just give someone a jar? – Mukhi Jun 24 '12 at 19:11
  • 1
    Yes, it should. I've used this technique to install drivers and other important files. It should be compiled into the jar and then you can access it with `Class.getResourceAsStream()` or `Class.getResource()` – gobernador Jun 24 '12 at 19:13

1 Answers1

2

Given that other kinds of contexts can be created from an InputStream, I would guess that it's a limitation of Excel, and that you won't be able to open the Excel file if it isn't on the file system.

Consider extracting the file from the jar and copy its content to a temporary file, possibly deleted when the application ends. See File.createTempFile() and File.deleteOnExit().

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • but how would I put the file in the jar? – Mukhi Jun 24 '12 at 19:21
  • How are you building the jar file? – JB Nizet Jun 24 '12 at 19:23
  • nevermind, I decided to convert the file to csv, and use the inputstream from there, as xls files are easily exported to csv. – Mukhi Jun 24 '12 at 19:33
  • 1
    So you know how to put a CSV file in a jar, but not an XLS file? What's the differene? A file is a file. – JB Nizet Jun 24 '12 at 19:38
  • Well with the cab I can use the getResourceAsStream method and it automatically adds it to the manifest, and keeps it in the jar. Can't do that with the xls – Mukhi Jun 25 '12 at 21:12
  • What you're saying doesn't make much sense. getResourceAsStream() gets a resource, at runtime, from the classpath. It doesn't have anything to do with a manifest, and doesn't add anything to it. If you build procedure adds the xls file to the jar, there is no problem accessing it with getResourceAsStream(). A jar is just a zip file. You can put wahtever file you want in it. – JB Nizet Jun 25 '12 at 21:29
  • when I put an xls file inside the jar, it doesn't launch, because there's no classpath in the manifest. when I put a csv it does the same, unless I use the getResourcesAsStream method, so I'm just assuming it automatically updates everything for me, as it anticipates it needs the file at runtime from inside the jar. – Mukhi Jun 26 '12 at 03:55
  • Besides which, that isn't the point. converting to csv, then adding it to the source folder, and using the one method, is clearly easier then whatever was going to happen. besides which, it's better for my situation to keep the file inside the jar, than create a temporary file, then delete it. – Mukhi Jun 26 '12 at 03:57
  • I repeat: the manifest has nothing to do with getResourceAsStream(). If the jar is in the classpath, and if the file is in the jar, getResourceAsStream() will find it, whatever the type or name of the file is. That said, what is your question now? Shouldn't you close it, since my answer doesn't suit you, and you don't seem to be interested in any other answer anyway? – JB Nizet Jun 26 '12 at 12:29
  • I can't because you've answered it. I've already flagged it for deletion, can't do anything else. – Mukhi Jun 30 '12 at 19:14