Link to home
Start Free TrialLog in
Avatar of MarteJ
MarteJ

asked on

Java Web Start and JAXB - problems with jaxb.properties file

Hi,

I am new to JWS, and am experiencing difficulties with the jnlp file. I am using JAXB to read an xml file in my application. I am having problems figuring out how to enter this file in the <resources> part of the jnlp file.

Here is part of the servlet that defines the jnlp file, with all the jar files needed. I need to add the jaxb.properties file somehow...?


out.println (" <resources>");
out.println (" <j2se version=\"1.4+\"");
out.println (" initial-heap-size=\"256m\"");
out.println (" max-heap-size=\"512m\"/>");
out.println (" <jar href=\"lib/myApplication.jar\"/>");
out.println (" <jar href=\"lib/jaxb-api.jar\"/>");
out.println (" <jar href=\"lib/jaxb-xjc.jar\"/>");
out.println (" <jar href=\"lib/jaxb-libs.jar\"/>");
out.println (" <jar href=\"lib/jaxb-impl.jar\"/>");
out.println (" <jar href=\"lib/jaxp-api.jar\"/>");
out.println (" <jar href=\"lib/xercesImpl.jar\"/>");
out.println (" <jar href=\"lib/xalan.jar\"/>");
out.println (" <jar href=\"lib/sax.jar\"/>");
out.println (" <jar href=\"lib/dom.jar\"/>");
out.println (" <jar href=\"lib/jax-qname.jar\"/>");
out.println (" <jar href=\"lib/namespace.jar\"/>");
out.println (" <jar href=\"lib/relaxngDatatype.jar\"/>");
out.println (" </resources>");

Please help, this is quite urgent!
Thankful for any help!!
Avatar of savalou
savalou

You could add the file to your myApplication.jar, couldn't you?
Avatar of MarteJ

ASKER

Yes, I have already tried that, but I get an error:

---

javax.xml.bind.JAXBException: Unable to locate jaxb.properties for package first_dir.second_dir.jaxb_dir
 
---

The error is caught for the following code in the class that reads the xml file:

---

JAXBContext jc = JAXBContext.newInstance("first_dir.second_dir.jaxb_dir");

---

I probably stated the question wrong, but I thought the error is caused because I had not placed the jaxb.properties file correctly in the jnlp file.
The JAXB code is compiled to first_dir.second_dir.jaxb_dir, where the jaxb.propterties file is created.

I cannot figure out why the file is not found. Any suggestions? Thanks!
Avatar of MarteJ

ASKER

I forgot to say that it works when the applications is run from command line, not through Java Web Start.

Greatful for any help!
>I thought the error is caused because I had not placed the jaxb.properties file correctly in the jnlp file.
>The JAXB code is compiled to first_dir.second_dir.jaxb_dir, where the jaxb.propterties file is created.

You don't have to do anything in the jnlp file about the jaxb.properties file.  

Are you sure the jaxb.properties file is in the myApplication.jar file and in the proper subdirectory, i.e., first_dir.second_dir.jaxb_dir?

ASKER CERTIFIED SOLUTION
Avatar of savalou
savalou

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MarteJ

ASKER

Sorry for a very late reply, but I have been struggeling with this some time.
First of all I found out that I was not running the latest version of JWSDP, but jwsdp-1.2. I have downloaded jwsdp-1.3 now, but my application will gives me the same exception running with Java Web Start. It works fine when running it as an ordinary application.

I tried out your great example, thank you so much! It took some time to merge your example into my deployment, but I got it to work (the example, that is). I will have to look more into my real application to find the problem.

Again - thank you for taking the time to test this for me and submitting the code!!! I learnt a lot!
Finding jaxb.properties

I am working with jaxb-1.0-beta on Win2000.

My JAXB application works just fine when my class files are in the appropriate folders in the file system.

I then wrap up my application in a jar file which includes the file com\mycompany\mypackage\jaxb.properties.

Now I get the infamous exception:
javax.xml.bind.JAXBException: Unable to locate jaxb.properties for package com.mycompany.mypackage

I have learned the following through Web Surfing.

 - jaxb-1.0-beta has a bug.
 - This bug is fixed, but not released.

So I did not bother to upgrade to the latest release of JAXB.

So let's fix it.  Dig deep enough and the problem is found at the following line.
   InputStream is = classLoader.getResourceAsStream( propFileName );

propFileName contains "com\ncems\premis\xml\jaxb\jaxb.properties"

This works just fine for finding the jaxb.properties file in the Win2000 file system.  It fails to find the jaxb.properties file in the jar file.

If I change the propFileName String to contain "com/ncems/premis/xml/jaxb/jaxb.properties", then it can find the jaxb.properties file in both the jar file and the Win2000 file system.

Time for a kluge.

The last point in my code, before hitting the exception, is the following line.
   JAXBContext jaxb = JAXBContext.newInstance("com.mycompany.mypackage");

newInstance comes in two flavors.  The other one lets me specify the ClassLoader to be used to find the jaxb.properties file.  So, I changed the above line to the following.
   JAXBContext jaxb = JAXBContext.newInstance("com.mycompany.mypackage", new JAXBClassLoader());

Now I just need a well behaved definition for JAXBClassLoader.  The following kluge works for me.

import java.io.InputStream;
import java.net.URL;

public class JAXBClassLoader extends ClassLoader {

  private ClassLoader d_classLoader = Thread.currentThread().getContextClassLoader();

  public InputStream getResourceAsStream(String name) {
    if (name.endsWith("jaxb.properties")) {
      name = name.replace('\\','/');
    }
    return d_classLoader.getResourceAsStream(name);
  }
}