1

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?

Community
  • 1
  • 1
toxin
  • 93
  • 1
  • 6

1 Answers1

1

User directory is tricky unless you are in control of the main program (as when it works for you).

I would suggest that you put the config file with the code itself (that's to say in src/main/resources. If you put it directly in that directory, you should be able to find it using

getClass().getResource("/ini.yaml");

Alternatively, you can put it in the parallel package (of the class where you need to find it) under resources and then you would use

getClass().getResource("ini.yaml");
Phasmid
  • 923
  • 7
  • 19
  • I updated my question, and I tried this. It seems not working. – toxin Sep 09 '14 at 07:41
  • 1
    I can't tell from your edited question exactly what your project structure is. Although there is some flexibility if you are in charge of the classpath (you are not), the standard and recommended way is this: project/src/main/resources/ini.yaml – Phasmid Sep 10 '14 at 13:01
  • I'm also a bit unclear exactly what you mean by running it as a service. Are you building then deploying a jar file, a war file, or what? How are you doing that? The directory structure I specified in previous comment will work for most (all) such deployments. – Phasmid Sep 10 '14 at 13:08
  • Now I put the conf under src, it works. I put the conf at the same level with "java resources" before, and maybe that make it not in the classpath. – toxin Sep 12 '14 at 02:30
  • I run it from tomcat in eclipse, so the path contains "eclipse". – toxin Sep 12 '14 at 02:31
  • You can check in Eclipse whether the resources folder has the "source folder" icon or just the folder icon. If the latter, then you can add it to your Build Path. However, if you are using Maven to build the project, then try "Update Project", the directory structure suggested is the default for Maven projects. – Phasmid Sep 12 '14 at 18:52