0

I created a function in java that verifies the existence of a file properties and if it exists, it retrieves its contents, otherwise it creates it by writing it inside. Starting this function from java application via eclipse, it runs correctly, while calling the function from jsp pages does not find the file and give an error.

this is my java function:

private static File fileName = new File("properties.properties");
Properties props = new Properties();

public void writeProperties() throws FileNotFoundException, IOException{        
    if(fileName.exists()){
        props.load(new FileInputStream(fileName));
    }

    boolean equal = false;
    if(!UtilsFunction.isEmpty(props.getProperty("table-list"))){
        String [] arr = props.getProperty("table-list").split(",");

        for(int i=0;i<arr.length;i++){
            if(arr[i].trim().equals(tableName)){
                equal = true;
                break;
            }
        }
        if(!equal){
            props.setProperty("table-list", props.getProperty("table-list")+","+tableName);
        }
    }else{
        props.setProperty("table-list", tableName);
    }

    OutputStream out = new FileOutputStream(fileName);
    props.store(out, "properties tables");
}

This is my JSP page:

<% ClassNameJava tableExecute = new ClassNameJava(tableName);%>
<% tableExecute.writeProperties(); %>

This is the error:

    HTTP Status [500] – [Internal Server Error]
    Type Exception Report

Message java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception

    java.io.IOException: java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Root Cause

    java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)
        java.io.FileInputStream.open0(Native Method)
        java.io.FileInputStream.open(Unknown Source)
        java.io.FileInputStream.<init>(Unknown Source)
        packageTableManager.TableExecute.writeProperties(TableExecute.java:233)
        org.apache.jsp.config_jsp._jspService(config_jsp.java:199)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Note The full stack trace of the root cause is available in the server logs.

    Apache Tomcat/8.

5.15
laze
  • 1
  • 1
  • So `File#exists` returns `true` and you can't read it? Do you have the right to read it? Where is the file relatively to the JSP (same dir?) –  May 25 '17 at 08:55
  • Related: https://stackoverflow.com/questions/25786334/java-file-exists-returns-true-even-when-file-doesnt-exist https://stackoverflow.com/questions/41237391/file-exists-returns-true-although-is-does-not-exist https://stackoverflow.com/questions/30520179/why-does-file-exists-return-true-even-though-files-exists-in-the-nio-files –  May 25 '17 at 08:57
  • in Java application fileName.exists() always returns true if file really exists; while in Jsp always return false – laze May 25 '17 at 09:06

1 Answers1

0

Try following code. Keep your properties.properties file at src package.

        Properties prop = new Properties();
        try
        {
            // load a properties file from class path, inside static method
            prop.load(ClassNameJava.class.getClassLoader().getResourceAsStream("properties.properties"));

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
Yogendra Joshi
  • 228
  • 1
  • 8
  • Run your IDE using administrator privileges . – Yogendra Joshi May 25 '17 at 09:46
  • private static File fileName = new File("properties.properties"); Properties props = new Properties(); These two lines keep in static block. – Yogendra Joshi May 25 '17 at 09:50
  • public static File fileName = new File("src/properties.properties"); i changed the function and the declaration of file with your solution; and it works fine; but i have an error on the outputstream. the error is java.io.FileNotFoundException: src\properties.properties (The system cannot find the path specified) – laze May 25 '17 at 11:46