2016-12-05 36 views
1

我有一個使用Apache Tomcat 6.0的簡單Web應用程序。我正在嘗試從路徑「resources/mysql.properties」中讀取屬性文件。這裏的「resources」文件夾位於「src」文件夾之外。當我試圖運行該項目作爲Java應用程序,它工作正常。但是當我在服務器上運行它時會引發FileNotFoundException。在Apache Tomcat上運行時發生FileNotFound異常?

這是帶有main的Java代碼,我在Console上運行它。

package com.jm.test; 

import com.jm.util.PropertyUtil; 

public class CodeTester { 

    public static void main(String[] args) { 
      System.out.println(PropertyUtil.getDBPropertyValue("driver")); 
    } 

} 

這是PropertyUtil.Java文件的代碼。

/** 
    * 
    */ 
    package com.jm.util; 

    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.util.MissingResourceException; 
    import java.util.Properties; 

    import org.apache.log4j.Logger; 

    /** 
    * @author Jaydeep Ranipa 
    * 
    */ 
    public class PropertyUtil { 
     public static final Logger log = Logger.getLogger(PropertyUtil.class); 
     private static final String resourceDir = "resources"; 

     private PropertyUtil() { 
     } 

     private static Properties loadProperties(String fileName) throws IOException, FileNotFoundException { 
      System.out.println("filename: "+fileName); 
      InputStream fileStream = new FileInputStream(new File(fileName)); 
      Properties props = new Properties(); 
      props.load(fileStream); 
      return props; 
     } 

     public static String getDBPropertyValue(String key) { 
      Properties prop = null; 
      String value = ""; 
      String fileName = "localmysql.properties"; 

      try { 
       prop = loadProperties(resourceDir + "/" + fileName); 
       value = prop.getProperty(key); 
       if (value == null) { 
        throw new MissingResourceException("Property not found.", "DATABASE", key); 
       } 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       System.out.println("properties file not found"); 
       e.printStackTrace(); 
       log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> not found."); 
       value = "Error occured."; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       System.out.println("properties file reading failed"); 
     log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> reading failed."); 
       value = "Error occured."; 
      } catch (MissingResourceException e) { 
       // TODO: handle exception 
       System.out.println("property not found"); 
       log.error("Property <<<"+e.getKey()+">>> for <<<"+e.getClassName()+">>> not found."); 
       value = "Error occured."; 
      } 
      return value; 
     } 

     public static String getErrorMessage(String errorCode) { 
      Properties prop = null; 
      String message = ""; 
      String fileName = "errormessage.properties"; 

      try { 
       prop = loadProperties(resourceDir + "/" + fileName); 
       message = prop.getProperty(errorCode); 
       if (message == null) { 
        throw new MissingResourceException("Property not found.", "ERROR", errorCode) 
       } 
      } catch (FileNotFoundException e) 
       // TODO Auto-generated catch block 
       log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> not found.") 
       message = "Something went wrong."; 
      } catch (IOException e) { 
       // TODO Auto-generated catch bloc 
       log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> reading failed."); 
       message = "Something went wrong."; 
      } catch (MissingResourceException e) { 
       // TODO: handle exception 
       log.error("Property <<<"+e.getKey()+">>> for <<<"+e.getClassName()+">>> not found."); 
       message = "Something went wrong."; 
      } 
      return message 
     } 
    } 

以下代碼給出了通過Web服務器執行時的錯誤。

Class.forName(PropertyUtil.getDBPropertyValue("driver")); 
    con = DriverManager.getConnection(PropertyUtil.getDBPropertyValue("url"), 
    PropertyUtil.getDBPropertyValue("username"), 
    PropertyUtil.getDBPropertyValue("password")); 
    return con; 

這裏是項目結構。 Project Structure

+0

您應該使用[Class.getResourceAsStream]加載屬性(http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String- )。 .jar或.war中的條目不是實際的文件,因此無法使用FileInputStream加載它。 – VGR

+0

嘗試直接調用文件名沒有resourceDir,看看是否有幫助。您也可以嘗試轉義斜線並嘗試。 – yogidilip

回答

0

在Tomcat的子目錄 「/ lib目錄」 添加driverDB。

+0

這可能會起作用,但不是一個好建議。不應將Tomcat的'bin'目錄用於應用程序特定的資源。 – ujulu

相關問題