我正在使用 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;
}