Ⅰ java web工程中讀取properties文件,路徑一直不知道怎麼寫
InputStreamin=getClass().getResourceAsStream("/config.properties");
在靜態方法中,由於不能使用getClass()方法,必須寫出類的名字。區別不大。
MyClass.class.getResourceAsStream("/config.properties");
使用這個方法,路徑前面可以加斜杠也可以不加。根據Class類getResourceAsStream()方法的JavaDoc:
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('u002e').
就是說,這個path假如以斜杠開頭,則斜杠後面的部分是文件相對classpath的路徑;假如不是,Java會把這個path看作是「包名/文件名」的結構,會嘗試在這個類的包裡面去找,而不是從classpath開始找;在這種情況下,除非你把properties文件放到MyClass.class所屬的包裡面,不然都會是null的。
MyClass.class.getClassLoader().getResourceAsStream("config.properties");
這是因為使用classloader進行讀取,所輸入的參數必須是一個相對classpath的絕對路徑,在格式上,一個絕對路徑是不能以'/'開頭的。
注意這兩個方法是同名的,但路徑參數的格式截然不同。
現在幾乎所有的web project都是maven project,Maven的默認設置是把
src/main/resources/
加入到classpath裡面的。那麼,最好的做法是把你的properties文件放進src/main/resources裡面,然後用上面代碼讀取。用Class類的,一般要加斜杠;用ClassLoader類的,絕不能加斜杠!
假如是Eclipse裡面,需要把這個src/main/resources加到classpath裡面。具體操作是右擊工程,選擇「Configure buildpath」,根據Maven的要求,把src/main/java和src/main/resources都加進去,並且保證Exclude是none,Include是all,或者至少要包括你需要讀取的文件。
Ⅱ 為什麼我的bootstrap用不了,我確定路徑沒錯,jq寫在bootstrap的前面,算上只用載入三個文件就好了吧
bootstrap前端框架本地引用:
<!--CSS樣式-->
<linkrel="stylesheet"href="common/styles/libs/bootstrap.css"><!--Bootstrap-->
<linkrel="stylesheet"href="common/styles/libs/font-awesome.css"><!--Fontawesomeicon-->
<!--js(注意jquery.js須在bootstrap.js的前面引用)-->
<scripttype="text/javascript"src="common/scripts/libs/jquery.js"></script>
<scripttype="text/javascript"src="common/scripts/libs/bootstrap.js"></script>
<linkrel="stylesheet"href="cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css"><!--Bootstrap(min壓縮版)-->
<scripttype="text/javascript"src="cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
<scripttype="text/javascript"src="cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
Ⅲ centos7 安裝boots,提示bash: ./bootstrap.sh: 沒有那個文件或目錄
要到腳本所在目錄才能這樣執行
不然就寫腳本的絕對路徑
Ⅳ 怎麼重新配置bootstrap.jar的路徑
可以雙擊該server配置。
1.錯誤症狀:右擊tomcat server,選擇start,出現下圖所示錯誤2.錯誤原因:我為了方便管理,把tomcat安裝到了當前的eclipse-project目錄下:E:/workspace/xxx_project/apache-tomcat-6.0.37,如下圖所示另外,也有可能是bootstrap.jar的路徑配置錯誤,可以雙擊該server配置,見解決方案3.解決方案 針對我目前的問題,把tomcat的安裝路徑挪到另一個位置即可,不要安裝到跟你的web-project的目錄在同一個位置(尤其是eclipse創建的project),例如E:/apache-tomcat-6.0.37
在計算機中,路徑是指用戶在磁碟上搜索文件時所經過的文件夾路徑。路徑分為絕對路徑和相對路徑。