在eclipse外部运行java jar需要bin dir中的文件
我正在使用eclipse进行开发,我正在使用一个API,该API要求文件位于/bin目录中(我不知道在java中这意味着什么路径)。我将我的应用程序导出为jar,并将所需的文件放在与jar文件相同的目录中,但当我使用终端运行应用程序时,应用程序无法定位文件。我无法理解这个问题 我正在使用以下命令运行应用程序:在eclipse外部运行java jar需要bin dir中的文件,java,eclipse,Java,Eclipse,我正在使用eclipse进行开发,我正在使用一个API,该API要求文件位于/bin目录中(我不知道在java中这意味着什么路径)。我将我的应用程序导出为jar,并将所需的文件放在与jar文件相同的目录中,但当我使用终端运行应用程序时,应用程序无法定位文件。我无法理解这个问题 我正在使用以下命令运行应用程序: java -jar app.jar 我还将终端目录更改为包含jar文件的目录,并尝试使用: java -cp . -jar app.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)
下面是简单的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");
}
创建jar时,Eclipse通常不放置资源/资产目录。错误消息是什么,有什么没有找到请向我们展示更多信息。我们可以帮助你的程度和你帮助我们的程度一样。你出口的jar的
MANIFEST.MF
里面有什么?检查classpath
entry。我编辑了这篇文章以包含更多的信息。如果您在Eclipse中显式地将文件复制到/bin中(顺便说一句,您绝对不应该这样做),您还需要将它们复制到运行时文件夹中(在第二个示例中,与.jar文件相同的文件夹)。
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;
}