Results 1 to 20 of 20
Thread: Accessing XML in a Jar
- 07-28-2010, 09:30 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Accessing XML in a Jar
Hello,
I am trying to access an xml document inside a jar and i cannot make it work. I am using Apache POI for accessing the documents.
My goal is to make a GUI application which can create/read/edit a xml file inside a jar. I have 3 class files : one for creating a xml, one for reading the file and one for the graphical interface.
I managed to make the applet work when not compressed but as soon as i make it into a jar i can't read the file anymore. From what i understood by reading from different threads i need to get the file as a resource but I haven't managed to do so. A little help would be appreciated.
This is the basic code for reading the document plus a few methods I've tried. As it is now, it works when not compressed to a jar. The document is created in the folder of the project. Also when I transform it into a jar it creates the file in the folder that the jar is.
Java Code:public class ReadExcel { public static String[] read(int n){ String[] s = new String[4]; ReadExcel unu=new ReadExcel(); try { InputStream is = ReadExcel.class.getResourceAsStream("excel.xls"); //POIFSFileSystem fs = new POIFSFileSystem( is ); //InputStream is =unu.getClass().getClassLoader().getResourceAsStream("/dir/excel.xls"); //FileInputStream fileInputStream = new FileInputStream("excel.xls"); HSSFWorkbook workbook = new HSSFWorkbook(is); HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
- 07-28-2010, 09:42 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
What errors are you getting?
Stack traces and full error description...
- 07-28-2010, 10:02 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
- 07-28-2010, 10:19 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Then log errors...because there will be an exception somewhere, possibly to do with write privileges (you can't write to a file in a jar), or simply not being able to locate the file. Since I can't see what you're doing in your catch block I can't tell if you're eating exceptions or not.
- 07-28-2010, 10:29 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
You can open (and read) your .jar file as if it were an ordinary .zip file. If your XML file is stored as X/Y/yourFile.xml you can treat the .jar file as the root so your .xml file can be found as /X/Y/yourFile.xml (note the leading slash character).
kind regards,
Jos
- 07-28-2010, 10:35 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Reading isn't a problem, though. The getResourceAsStream should work, assuming the path is correct. Still, without the exception that I suspect is happening in there it's hard to tell.
- 07-28-2010, 11:55 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Sorry about that:
I think I know what you mean but the xml already is in root directory of the jar. I also tried to move it around in other folders but I still couldn't get it to run.Java Code:public class ReadExcel { public static String[] read(int n){ String[] s = new String[4]; ReadExcel unu=new ReadExcel(); try { InputStream is = ReadExcel.class.getResourceAsStream("excel.xls"); //POIFSFileSystem fs = new POIFSFileSystem( is ); //InputStream is =unu.getClass().getClassLoader().getResourceAsStream("/dir/excel.xls"); //FileInputStream fileInputStream = new FileInputStream("excel.xls"); HSSFWorkbook workbook = new HSSFWorkbook(is); HSSFSheet worksheet = workbook.getSheet("POI Worksheet"); ................ .................. } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return s; public static void main(String[] args) { // main class is empty }
I have a problem. I am on a company computer and I don't have full access to run all applications so i can't get the Java Console to start and see the errors.
- 07-28-2010, 12:04 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Can you set up log4j to log to a file?
Or simply change this bit so it logs to a file, manually...ie a FileWriter (Buffered) to a file on a fixed location on that machine.
If it's in the root directory then I think it should start with a '/'? I wouldn't quote me on that, though, because that's one of things I always have to look up and try out...:)
ETA: By which I mean it should be "/excel.xls".
- 07-28-2010, 12:44 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
I managed to do it from command prompt :
My guess is that it cannot find the file, hence the Unknown source. Tried also with /excel.xls, same thing.Java Code:[SIZE="2"]D:\Documents and Settings\siltabo\Desktop>java -jar interfata.jar Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apach e/poi/hssf/usermodel/HSSFWorkbook at test.ReadExcel.read(ReadExcel.java:29) at test.Interfata$2.actionPerformed(Interfata.java:46) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) D:\Documents and Settings\siltabo\Desktop>[/SIZE]Last edited by WizzRD; 07-28-2010 at 12:51 PM.
- 07-28-2010, 12:50 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
It's because you haven't got the apache-poi jar (poi-somthing-something.jar and dependencies) mentioned in your Manifest, or deployed where your Manifest says it's deployed. It hasn't got to the point of trying to read the file yet.
What IDE are you using to build this?
- 07-28-2010, 12:52 PM #11
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Eclipse...
- 07-28-2010, 01:11 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Your .jar file depends on the org.apache.mumble.jar file (and maybe some other .jar file(s) as well). You should tell the jvm that; you have to create a manifest file in your .jar file that tells it so with the line:
kind regards,Java Code:Class-Path: org.apache.mumble.jar other.jar yetAnother.jar
Jos
- 07-28-2010, 01:26 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Essentially any jars that you have had to tell Eclipse to use when you had Eclipse run this.
- 07-28-2010, 02:56 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Yep, that was the problem, the class path. I got it to work but only by putting the created jar file in the same directory with the external jar that i have used.
But the idea is to make the program work from 1 file only, so I need to include the jar which contains the extra libraries inside my jar but i don't know how to indicate this in the manifest.
Inside my jar i have 2 folders. one META-INF and one called test. The extra jar is in that folder. I have tried in the manifest " Class-Path: test/poi-3.6-20091214.jar " and also with the reversed slash but no go.
If you have any idea it is welcome, if not thank you for all your help, it was well appreciated. ;)
- 07-28-2010, 03:05 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You can't.
That's not the idea with jars.
Netbeans, for example, creates a dist directory with the idea that you zip that up, and deploy it by unzipping. Everything needed is then in the correct place.
- 07-28-2010, 03:06 PM #16
Remove all non standard classes from your program. Use only classes that come in the JDK.the idea is to make the program work from 1 file only
Otherwise if you need classes from other packages,
either provide the jar that contains those classes and add it to the classpath
or extract the needed classes from whatever jar they are in and put them in your jar.
I don't think the JVM will look in one jar for another jar.
- 07-28-2010, 03:08 PM #17
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
- 07-28-2010, 03:33 PM #18
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
- 07-28-2010, 04:02 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Extracting really isn't a solution.
Honestly.
For starters how do you deploy a new version of a 3rd party lib? Deploy the whole app?
- 07-29-2010, 12:20 PM #20
Member
- Join Date
- Jul 2010
- Posts
- 9
- Rep Power
- 0
Yeah, it doesn't really work except in maybe a few cases. For me, even if i extract the classes that i use, they are part of a package and to function they import other classes from the package so, I would basically have to decompile and edit a lot files. Easier if i just let them in the jar and use the jar externally.
Similar Threads
-
Accessing Web service
By madhu_sushmi in forum Advanced JavaReplies: 29Last Post: 03-09-2010, 06:40 PM -
accessing variables
By emp in forum New To JavaReplies: 3Last Post: 04-23-2009, 04:36 AM -
Accessing USB
By FieryProphet in forum NetworkingReplies: 3Last Post: 12-13-2008, 05:29 AM -
problem in accessing EJB
By parimal in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 08-07-2008, 03:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks