Results 1 to 20 of 20
  1. #1
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default 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");

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    What errors are you getting?
    Stack traces and full error description...

  3. #3
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What errors are you getting?
    Stack traces and full error description...
    I'm not getting any errors. It works when i run it from Eclipse. The problem is that is doesn't work when i run it as a jar.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    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.

  7. #7
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.
    Sorry about that:

    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
    
    }
    Quote Originally Posted by JosAH View Post
    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).
    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.


    Quote Originally Posted by Tolls View Post
    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.
    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    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".

  9. #9
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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".
    I managed to do it from command prompt :

    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]
    My guess is that it cannot find the file, hence the Unknown source. Tried also with /excel.xls, same thing.
    Last edited by WizzRD; 07-28-2010 at 12:51 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    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?

  11. #11
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Eclipse...

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by WizzRD View Post
    Eclipse...
    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:

    Java Code:
    Class-Path: org.apache.mumble.jar other.jar yetAnother.jar
    kind regards,

    Jos

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    Essentially any jars that you have had to tell Eclipse to use when you had Eclipse run this.

  14. #14
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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:

    Java Code:
    Class-Path: org.apache.mumble.jar other.jar yetAnother.jar
    kind regards,

    Jos
    Quote Originally Posted by Tolls View Post
    Essentially any jars that you have had to tell Eclipse to use when you had Eclipse run this.
    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. ;)

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    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.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    the idea is to make the program work from 1 file only
    Remove all non standard classes from your program. Use only classes that come in the JDK.
    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.

  17. #17
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.
    Got it. Thank you.

  18. #18
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Remove all non standard classes from your program. Use only classes that come in the JDK.
    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.
    I will try this when I get home. Thank you.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,758
    Rep Power
    19

    Default

    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?

  20. #20
    WizzRD is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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?
    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

  1. Accessing Web service
    By madhu_sushmi in forum Advanced Java
    Replies: 29
    Last Post: 03-09-2010, 06:40 PM
  2. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 04:36 AM
  3. Accessing USB
    By FieryProphet in forum Networking
    Replies: 3
    Last Post: 12-13-2008, 05:29 AM
  4. problem in accessing EJB
    By parimal in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-07-2008, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •