I am working on a web service and I want to use yaml as my config file.
But the problem is, when I run the main function it is ok , but when I run it by calling the service, the file path becomes different and the file can't be found.
code is here:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import org.yaml.snakeyaml.Yaml;
public class ConfigInstance{
private static String projectRoot = System.getProperty("user.dir");
private final static String confRoot = "/conf";
private static CloudConfig cloudConfig = null;
public static CloudConfig getCloudConfig() throws FileNotFoundException{
if(null == cloudConfig){
Yaml yaml = new Yaml();
System.out.println(projectRoot+confRoot);
InputStream input = new FileInputStream(new File(projectRoot+confRoot + "/ini.yaml"));
cloudConfig = yaml.loadAs( input, CloudConfig.class );
}
return cloudConfig;
}
public static void main(String args[]){
try {
CloudConfig cc = ConfigInstance.getCloudConfig();
System.out.println(cc);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I run the main, I get:
/home/jiaohuan/workspace/SwitchCloud/conf
CloudConfig [cloudIp=10.108.119.165]
When I call the web service(use cxf), I get:
/home/jiaohuan/Desktop/eclipse/conf
and I get the java.io.FileNotFoundException.
My project struct is:
project |
---> java resources |
-------> src |
-----------> packages |
-------> test |
---> conf |
------> ini.yaml |
And I have tried this : Java - FilenotfoundException for reading text file , it doesn't work.
So I want to solve the problem, any idea?