0
我正在開發中使用eclipse,我正在使用一個需要文件在/ bin目錄下的API(這個路徑在java中的含義我不知道) 。我將應用程序導出爲jar文件,並將所需的文件放在與jar文件相同的目錄中,但是當我使用Terminal運行應用程序時,應用程序無法找到這些文件。我無法理解這個路徑問題。在eclipse上運行java jar需要bin目錄下的文件
我正在使用此命令的應用程序:
java -jar app.jar
我也改變了終端目錄複製到包含JAR文件的目錄,我試着用:
java -cp . -jar app.jar
這沒」工作。
編輯: 錯誤是應用程序無法找到所需的文件。在Eclipse中,我必須將這些文件放在/ bin目錄中,以便API找到它們。
下面是完整的異常:
Exception in thread "main" java.io.IOException: failed to find resource /cmu/arktweetnlp/50mpaths2
at cmu.arktweetnlp.util.BasicFileIO.getResourceReader(BasicFileIO.java:233)
at cmu.arktweetnlp.impl.features.WordClusterPaths.<init>(WordClusterPaths.java:29)
at cmu.arktweetnlp.impl.features.FeatureExtractor.initializeFeatureExtractors(FeatureExtractor.java:146)
at cmu.arktweetnlp.impl.features.FeatureExtractor.<init>(FeatureExtractor.java:30)
at cmu.arktweetnlp.Tagger.loadModel(Tagger.java:39)
at com.POSTest.main(POSTest.java:22)
終端內容:
MacBook-Pro:ArabicTwitterEye ma$ ls
CSVArabicTwitterEye.jar log resources
cmu profiles tweets.csv
MacBook-Pro:ArabicTwitterEye ma$
下面是簡單的Java代碼:
public static void main(String[] args) {
Tagger tagger = new Tagger();
String modelFilename = "model.20120919";
System.out.println("Loading model ...");
try {
tagger.loadModel(modelFilename);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done loading model.");
}
下面是來自API源代碼:
public class WordClusterPaths implements FeatureExtractorInterface {
/** TODO this should be moved into config somehow **/
public static String clusterResourceName = "/cmu/arktweetnlp/50mpaths2";
public static HashMap<String,String> wordToPath;
public WordClusterPaths() throws IOException {
// log.info("Loading clusters");
//read in paths file
BufferedReader bReader = BasicFileIO.getResourceReader(clusterResourceName);
String[] splitline = new String[3];
String line=BasicFileIO.getLine(bReader);
wordToPath = new HashMap<String,String>();
while(line != null){
splitline = line.split("\\t");
wordToPath.put(splitline[1], splitline[0]);
line = BasicFileIO.getLine(bReader);
}
// log.info("Finished loading clusters");
}
而且檢查:
public static BufferedReader getResourceReader(String resourceName) throws IOException {
assert resourceName.startsWith("/") : "Absolute path needed for resource";
InputStream stream = BasicFileIO.class.getResourceAsStream(resourceName);
if (stream == null) throw new IOException("failed to find resource " + resourceName);
//read in paths file
BufferedReader bReader = new BufferedReader(new InputStreamReader(
stream, Charset.forName("UTF-8")));
return bReader;
}
什麼是錯誤消息,它沒有找到 –
請向我們展示更多信息。只有你幫助我們,我們才能幫助你。 – Jashaszun
您導出的jar的'MANIFEST.MF'裏面有什麼?檢查'Class-Path'條目。 –