-1

I am currently developing a web application, I have an XML file named File.xml containing PostgreSQL connection parameters: login, password, ip_address, pilote, port, bdd. . I added this file to the bin folder of Apache Tomcat. I have class.jav which use the parameters of XML file to connect to the data base.

When I run the class I get the result of the else line (File not found). I doubt that the class does not access the File.xml under the apache bin folder. what to do?

    private ParamBDD()
{
    SAXBuilder sxb = new SAXBuilder();
    try
    {
        mes_documents mes=new mes_documents();
        Fichier fichier = new Fichier();
        File file=new File("File.xml");
        //File file=new File(this.getClass().getResource("/resources/Fichiers_parametres/parametreconnexion_crypte.xml").getFile());



            String str_fichier ="File.xml";


            if (file.isFile())
            {
                org.jdom.Document document = sxb.build(new File(str_fichier));

                Element racine = document.getRootElement();
                List listParam = racine.getChildren("param");

                Iterator i = listParam.iterator();
                while (i.hasNext())
                {
                    Element courant = (Element) i.next();

                    pilote = courant.getChild("pilote").getText().trim();
                    utilisateur = courant.getChild("login").getText().trim();
                    password = courant.getChild("password").getText().trim();
                    adresseIP = courant.getChild("adresseIP").getText().trim();
                    port = courant.getChild("port").getText().trim();
                    BDDGenerale = courant.getChild("bdd").getText().trim();
                    System.out.println("BDD Generale:"+BDDGenerale);

                    //JOptionPane.showMessageDialog(null, "Fin du fichier","",JOptionPane.INFORMATION_MESSAGE);
                }

            }
            else JOptionPane.showMessageDialog(null, "le fichier contenant les parametres n'existe pas","",JOptionPane.INFORMATION_MESSAGE);

    }
rolfl
  • 17,539
  • 7
  • 42
  • 76
Asma Touihri
  • 21
  • 1
  • 2
  • 3
    Please translate your question to English. – Jesper May 09 '15 at 10:01
  • Hi Asma, and welcome to StackOverflow. Can you edit your post and ask your question [in English](http://blog.stackoverflow.com/2009/07/non-english-question-policy/)? – das-g May 09 '15 at 10:02
  • I am currently developing a web application, I have an xml file named File.xml contains connection parameters to postgresql database . I added this file to the bin folder of apache. i have class.jav which use the parametrs of xml file to connect to data base. when i run the class i get the result of the else line. I doubt that the class does not access the File.xml under the apache bin folder. what to do? – Asma Touihri May 09 '15 at 10:06
  • Just translated. Asma, check your braces. – Patrick May 09 '15 at 10:08
  • If your `File file = ...` line is really the commented out line, then that will be your real problem. If the `File file = ....` line is the one you currently have as `= new File("File.xml");` then your problem is because you do not have a file called `File.xml` in your current directory. Which `file` is it? – rolfl May 09 '15 at 10:25
  • no my real file is File.xml and i added it to the bin folder of the apache server – Asma Touihri May 09 '15 at 10:41

1 Answers1

3

All relative file paths in Java are resolved relative to the directory the user was in when they started the Java program. The directory you were in when you started the program is recorded in the system property: System.getProperty("user.dir");

So, if you were, for example, in /home/asma/myproject when you started tomcat, then the user.dir folder will be /home/asma/myproject

Then, when you have a File reference, like:

File file = new File("File.xml");

then the Java system will look for the file /home/asma/myproject/File.xml

What you need to do is either make the file reference an absolute path, or ensure you are in the bin folder when you actually start tomcat... (which is a pain... so don't do that).

Change your code to:

File file = new File(System.getProperty("catalina.base") + "/bin/File.xml");
rolfl
  • 17,539
  • 7
  • 42
  • 76