1. 程式人生 > >eclipse中的tomcat,程式碼中建立file獲取目錄與tomcat單獨執行獲取目錄不同的解決辦法

eclipse中的tomcat,程式碼中建立file獲取目錄與tomcat單獨執行獲取目錄不同的解決辦法

提示,此程式碼所在的都是rest介面(web專案)中,而不是在普通的Test類或者是main方法中 重現:
    @GET     public Response sayHello(@Context HttpServletRequest request,             @Context HttpServletResponse response){         File file=new File("repository/configuration.xml");         URI uri = file.toURI();         System.out.println(uri);         return Response.ok().status(200).build(); }


返回結果為: file:/E:/works/eclipse-jee-mars-2-win32-x86_64/eclipse/repository/configuration.xml 而我要獲取的是tomcat下的目錄,如我上端程式碼,直接啟動tomcat執行獲取到的是: file:/N:/works/IDE/apache-tomcat-7.0.54-windows-x86/apache-tomcat-7.0.54/bin 那麼就要修改配置,讓在eclipse中也獲取此目錄 解決辦法: 1、雙擊Servers下的tomcat
2、將會開啟eclipse中的tomcat配置介面,然後點選“Open launch configuration”

3、開啟標籤頁Arguments,選擇“Working  directory“為Other,填入如下值:     N:\works\IDE\apache-tomcat-7.0.54-windows-x86\apache-tomcat-7.0.54\bin     然後儲存即可 如圖:
修改過後,再在eclipse中啟動tomcat,執行此程式碼獲取到的就是和tomcat執行時獲取的目錄相同了 file:/N:/works/IDE/apache-tomcat-7.0.54-windows-x86/apache-tomcat-7.0.54/bin