[英]getSystemResourceAsStream unable to find resource
我需要從文件系統中讀取XML文件。
讀取XML的代碼在jar文件中,我無法修改該代碼
ClassLoader.getSystemResourceAsStream('Config.xml');
我嘗試在類路徑以及命令行路徑中設置Config.xml的位置
SET CLASSPATH=C:\opt\conf
和
SET PATH=%PATH%;C:\opt\conf
但是沒有任何效果。
每次我運行代碼時,都會得到異常
Caused by: java.io.IOException: Could not find resource Config.xml
在如何從文件系統讀取此config.xml方面需要一些幫助。 我需要找到一種方法將文件放置在某個位置或設置類路徑,以使我無法修改的代碼可以讀取config.xml。
我已經在Google上搜索了很多,並試圖實施解決方案,但是沒有任何效果。
這是一種從罐子中讀取思想的方法:
public Object[] readJARFiles(String jarName, String suffix) throws FileNotFoundException{
jarName.replaceAll("/", "\\.");
ArrayList<Object>objects = new ArrayList<Object>();
File f;
f = new File(jarName);
f = new File(jarName);
if(f.exists()) {
try {
JarInputStream jarFile;
jarFile = new JarInputStream(new FileInputStream (jarName));
JarEntry jarEntry;
while(true) {
jarEntry = jarFile.getNextJarEntry();
if(jarEntry == null) {
break;
}
if(jarEntry.getName().endsWith(suffix)){
objects.add(jarEntry);
}
/* Object tempObject;
tempObject = Class.forName(jarEntry.getName().
replaceAll("/", "\\."));
tempObject.substring(0, jarEntry.
getName().length() - 6));*/
}
}catch( Exception ex){
System.out.println("Error! in class ReadFiles -- " + ex.toString());
System.out.println();
}
Object[] returnArray = new Object[objects.size()];
System.out.println(returnArray.length);
for(int n = 0; objects.size() < n; ++n) {
returnArray[n] = objects.get(n);
}
return returnArray;
} else {
System.out.println("There aren't files to read");
return null;
}
}
我能夠使用其他方法來做到這一點。
在執行jar的shell腳本中,我添加了一條命令,以在執行jar之前將jar文件更新為包含config.xml。
jar uf encrypt.jar Config.xml
java -jar encrypt.jar P password
如果不存在,這會將Config.xml
添加到jar中;如果存在,則將更新。 然后,當我執行jar時, Config.xml
將在jar中可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.