1

I'm trying to use Jaxb in order to unamrshal an xml file. for some reason that I don't understand, I cannot refer to any other location then a full path on my specific computer. In the code below, the commented line doesn't work, but only the one above it. the file does exist (in two locations) and the commented line does work on a diffrent class.

JAXBContext jc = JAXBContext.newInstance(Monopoly.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
//unmarshaller.unmarshal(new File("resources/monopoly_config.xml" ));
unmarshaller.unmarshal(new File( "C:\\Users\\Lior\\Documents\\NetBeansProjects\\Monopoly curr\\MonopolyServer\\src\\BoardInfoResources\\monopoly_config.xml"));
skaffman
  • 398,947
  • 96
  • 818
  • 769
Lior
  • 15
  • 1
  • 3

1 Answers1

1

UPDATE

Since you are deploying to a server (from your comments), why not load your XML using a ClassLoader? In a server environment you wouldn't be able to count on the File object in the way that you want to (as you have already discovered):

ClassLoader cl = Monopoly.class.getClassLoader();
InputStream xml =
 cl.getResourceAsStream("resources/monopoly_config.xml");
JAXBContext jc = JAXBContext.newInstance(Monopoly.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Monopoly monopoly = (Monopoly) unmarshaller.unmarshal(xml);

JAXB will allow you to unmarshal any valid file object. And it will definitely unmarshal files created with relative paths (see answer below for an example):

In your example you will need to make sure that your working directory is set correctly. Based that your full path is:

"C:\\Users\\Lior\\Documents\\NetBeansProjects\\Monopoly curr\\MonopolyServer\\src\\BoardInfoResources\\monopoly_config.xml"

Assuming your working directory is:

"C:\\Users\\Lior\\Documents\\NetBeansProjects\\Monopoly curr\\MonopolyServer\\src\\"

Your relative path appears to be wrong (since there is no resources directory):

"resources/monopoly_config.xml"

You may have meant it to be:

"BoardInfoResources/monopoly_config.xml"
Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 2
    This is the answer. Just to add to it, though, is you can check your current working directory by doing: System.out.println(System.getProperty("user.dir")); – Alvin May 25 '11 at 17:52
  • Thank you for that. unfortunately, this doesn't solve it. Sorry if I wasn't clear earlier - both resources and BoardInfoResources exist and contain the xml, however, it will simply not work unless it's a full path. – Lior May 25 '11 at 19:19
  • @Lior - Did you check your working directory as suggested by Alvin? – bdoughan May 25 '11 at 19:22
  • yap, it is C:\Users\Lior\Documents\NetBeansProjects\Monopoly curr\MonopolyServer. I was able to make it run "on it's own", but when I try to deploy it to my server I get "java.io.FileNotFoundException: C:\apache-tomcat-6.0.32\bin\src\BoardInfoResources\monopoly_config.xml (The system cannot find the path specified)]" – Lior May 25 '11 at 19:48
  • @Lior why don't you load it from the ClassLoader instead? See my updated answer for details. – bdoughan May 25 '11 at 19:53
  • Any tips on how to avoid the need to restart netbeans, re install axis and rebooting so often? I keep getting to a point that I have to take these actions because deploy doesn't seem to effect.. – Lior May 26 '11 at 07:54
  • @Lior - You may want to start a new question about this last issue, that may bring in the NetBeans and Axis people. – bdoughan May 26 '11 at 13:20