Results 1 to 6 of 6

Thread: Relative xml file with application

  1. #1
    Join Date
    Jun 2009
    Posts
    105

    Relative xml file with application

    I can not retrieve a xml file with my application. By testing my application with NetBeans, the file is found correctly. But when I run the software after construction, it does look over the file based on the Java executable. Here's the code:

    I get my file like this:
    Code:
    sxb.build document = (new File ( "data.xml"));
    The file is physically located at the root folder of my project. So, as mentioned above, when I run my project in NetBeans, no worries. But when I run the application after construction, it finds the file in my home directory:
    Code:
    /home/bux/data.xml
    Same thing if I specify "./data.xml" (he looks at /home/bux/./data.xml). How can I do? And is this normal?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Relative xml file with application

    But when I run the software after construction, it does look over the file based on the Java executable.
    Relative paths are relative to the working directory, which can vary depending on how you run the program. So it's quite normal!

    So to include in the directory of the application built, save the file in your classpath.

    Another way is to create an environment variable such variable_env = directory, or you'll drop your file. And in your application you recover your variable so your directory and you use your file. Although it is expected the good rights on the directory and file.

  3. #3
    Join Date
    Jun 2009
    Posts
    105

    Re: Relative xml file with application

    Quote Originally Posted by !const View Post
    Relative paths are relative to the working directory, which can vary depending on how you run the program. So it's quite normal!

    So to include in the directory of the application built, save the file in your classpath.

    Another way is to create an environment variable such variable_env = directory, or you'll drop your file. And in your application you recover your variable so your directory and you use your file. Although it is expected the good rights on the directory and file.
    Create a kind of package and place the file in question? How can I then point to the file? I can see this:

    To set the environment variable classpath

    * Under Linux, use the following command:
    export CLASSPATH = <directory>: <directory>; ...

    For example:
    export CLASSPATH = / home / jeff / java: / usr / lib / jdkx.xx / lib / classes.zip ...

    * Under Windows, use the following command:
    SET CLASSPATH = <directory>; <directory>; ...

    For example:
    SET CLASSPATH = c: \ java c: \ jdkx.xx \ lib \ classes.zip; ...

    Can I do this in a manner relative to the executable software?

    Finally do not get big a pointer on my data.xml file is really the concern that blocks the use of my software

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Relative xml file with application

    To retrieve your file you can do

    Code:
    String nameFichier = Thread.currentThread().getContextClassLoader().getResource("afile.xml").getFile()

  5. #5
    Join Date
    Jun 2009
    Posts
    105

    Re: Relative xml file with application

    I tried this:

    Code:
    try {
     document = sxb.build(new File(Thread.currentThread().getContextClassLoader().getResource("data.xml").getFile()));
     } catch(Exception e){
    JOptionPane.showMessageDialog(null,"Can not find file data.xml ("+e+")");
    }
    and got this:

    Can not find the file data.xml (java.lang.NullPointerException)

  6. #6
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Relative xml file with application

    I also tested
    Code:
    import java.io.File;
     
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
     
    public class BuildWithSaxBuilder {
    	static org.jdom.Document document;
    	static Element root;
    	public static void main(String[] args) {
    		String nameFile = Thread.currentThread().getContextClassLoader().getResource("file.xml").getFile();
    		  SAXBuilder sxb = new SAXBuilder();
    		      try
    		      {
    		         document = sxb.build(new File(nameFile));
    		         System.out.println("document" + document);
    		      }
    		      catch(Exception e){ //bla bla bla }
    		   }
    	}
    And it works. If your SXB is instantiated then your problem is elsewhere. Is your file.xml in your classpath (or is it .java or .class)?

Similar Threads

  1. Replies: 2
    Last Post: 23-02-2012, 02:56 PM
  2. Replies: 1
    Last Post: 06-01-2012, 08:38 PM
  3. Replies: 4
    Last Post: 05-01-2012, 04:40 PM
  4. Better amongst the absolute or relative path in HTML
    By Galeny in forum Software Development
    Replies: 3
    Last Post: 23-12-2009, 10:54 PM
  5. Creating Relative shortcut in windows
    By ADELIO in forum Operating Systems
    Replies: 3
    Last Post: 22-08-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,951,078.59209 seconds with 17 queries