我正在創建一些REST API。爲此我有一些應用程序級別的通用名稱值對。爲了配置這些名稱值對,我只是在WebContent中創建了一個xml文件,並從類的靜態塊中的xml中訪問了值,並使用靜態變量進行了初始化。因此,在xml中爲每個名稱給出的所有值將分配給來自類的靜態塊的相應靜態變量。無法訪問xml文件
我能夠訪問這些變量並獲得值比從REST API客戶端等各個類別。問題出現了,當我創建一個REST API客戶端來使用在同一個項目中創建的API時,FileNotFoundException會拋出我爲我的xml文件(WebContent/myxml.xml)提供的路徑。
我可以看到,它在尋找我日食路徑(/home/aneesh/eclipse/WebContent/myxml.xml)相同的XML文件。和FileNotFoundException拋出。 我該如何解決這個問題?
1. class which accessing xml file
class Constants {
public static String name;
static {
initializeConstants();
}
public static void initializeConstants() {
try {
//initializing Constants
//"Reading file"
File xmlFile = new File("WebContent/myxml.xml");
.......
//file is getting read perfectly and "name" is initialized
}
}
2. class accepting static variable Constants.name
// accepting value of 'name' using Constants.name successfully
// writing a method which is accepting some parameters and using Constants.name.
// writing a "main" method and calling this class's methods will work perfectly.
// value of Constants.name will be accessible here.
3. REST API created which will call methods of second class with parameters.
4. Webscript client created for consuming above created API.
// Here the exception is coming.
// Eception is throwing from the class Constants
//Exception from Constants : FileNotFoundException
java.io.FileNotFoundException: /home/aneesh/eclipse/eclipse/WebContent/myxml.xml (No such file or directory)
那麼,爲什麼在這種情況下它尋找日食的路徑中的文件?如何解決它? 也試過把src文件夾放進去,但是不工作。
請添加一些代碼。發生FileNotFoundException很可能是因爲確實沒有文件。再次檢查你的絕對路徑和相對路徑。 – snrlx