2017-04-04 44 views
0

我在eclipse中設置VM參數爲-DFilePath =「C:\ file \ txt」 但是在java中調用這個#FilePath#時,它將輸出作爲C:filetxt的C:\ file \ txt。這導致文件未找到異常。任何人都可以請幫助我在這..Eclipse VM參數選擇不帶分隔符的目錄名

+0

設置參數怎麼樣? –

+0

@ greg-449嗨,格雷格,我在日食中有主類。我右鍵單擊並單擊運行As - >運行配置,從那裏我將選擇參數選項卡,然後我將添加虛擬機參數。 – Vinoth

回答

0

問題必須在你如何「調用這個#FilePath#」。

我用下面的代碼進行測試:

package test; 

import java.io.File; 

public class EnvPath { 

    public static void main(String[] args) { 
     String path = System.getProperty("FilePath"); 
     System.out.println("Prop: " + path); 
     File file = new File(path); 
     System.out.println("File: " + file); 
    } 
} 

從啓動Eclipse,如你所描述的,或java -DFilePath="C:\file\txt" test.EnvPath使用Windows命令提示符並使用GNU bash的 - 它總是產生:

Prop: C:\file\txt 
File: C:\file\txt 
+0

嗨,卡洛斯。感謝您的好解釋。我懷疑是不是使用System.getProperty(「FilePath」); ,我們正在嘗試使用一些替代變量,它將從虛擬機參數中獲得實際值。舉例說明:我的一個屬性的鍵值對如下所示:「TextfilePath =#filePath#\ today \ file」。所以當我嘗試獲取此屬性字符串filePath = prop.get(TextfilePath); filePath變量的值應該是「C:\ file \ txt \ today \ file」。但它的價值在於「C:filetxt \ today \ file」。 – Vinoth

+0

會更好地將這個(和一些代碼)添加到問題中......很難猜測'prop'和'get'方法是什麼。 –