我正在使用eclipse开发,我正在使用一个API,要求文件位于/ bin目录中(这个路径在java中我的意思是不知道)。我将我的应用程序导出为jar并将所需文件放在与jar文件相同的目录中,但是当我使用终端运行应用程序时,应用程序无法找到文件。我无法理解这个路径问题。
我正在使用此命令运行应用程序:
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;
}
答案 0 :(得分:0)
在创建jar时,Eclipse通常不会放置资源/资产目录。