Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
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?
Reply With Quote
  #2  
Old 09-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Relative xml file with application

Quote:
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.
Reply With Quote
  #3  
Old 09-11-2009
Member
 
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
Reply With Quote
  #4  
Old 09-11-2009
Member
 
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()
Reply With Quote
  #5  
Old 09-11-2009
Member
 
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:

Quote:
Can not find the file data.xml (java.lang.NullPointerException)
Reply With Quote
  #6  
Old 09-11-2009
Member
 
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)?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Relative xml file with application"
Thread Thread Starter Forum Replies Last Post
What is the formula to find Relative Percent Difference in Microsoft Excel LuniA MS Office Support 2 23-02-2012 02:56 PM
How to create formula to find relative percent difference in Excel Aarti C Windows Software 1 06-01-2012 08:38 PM
Is it possbile to calculate RSD ( relative standard deviation) in Mircosoft Excel KalaK Windows Software 4 05-01-2012 04:40 PM
Better amongst the absolute or relative path in HTML Galeny Software Development 3 23-12-2009 10:54 PM
Creating Relative shortcut in windows ADELIO Operating Systems 3 22-08-2009 09:35 AM


All times are GMT +5.5. The time now is 08:17 AM.