【问题标题】:JAVA - path issue (works in eclipse, not in cmd)JAVA - 路径问题(在 eclipse 中工作,而不是在 cmd 中)
【发布时间】:2021-06-02 22:02:53
【问题描述】:

为什么以下起始在 eclipse 中有效:

private static MaxentTagger maxentTagger = new MaxentTagger("c:\\DP\\lemma\\models\\english-left3words-distsim.tagger");

但在命令行中它会抛出:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.ExceptionInInitializerError
        at dp.beans.MySearch.<init>(MySearch.java:122)
        at dp.runable.Main.main(Main.java:25)
        ... 5 more
Caused by: java.lang.IllegalArgumentException: name
        at sun.misc.URLClassPath$Loader.findResource(Unknown Source)
        at sun.misc.URLClassPath.findResource(Unknown Source)
        at java.net.URLClassLoader$2.run(Unknown Source)
        at java.net.URLClassLoader$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findResource(Unknown Source)
        at java.lang.ClassLoader.getResource(Unknown Source)
        at java.net.URLClassLoader.getResourceAsStream(Unknown Source)
        at edu.stanford.nlp.io.IOUtils.findStreamInClasspathOrFileSystem(IOUtils.java:370)
        at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:399)
        at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:646)
        at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:284)
        at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:248)
        at dp.data.Settings.<clinit>(Settings.java:80)
        ... 7 more

Settings.java:80对应MaxentTagger 发起..

是否有不同的方式来声明 windows 路径,在 eclipse 和 cmd 中都可以使用?

更新findStreamInClasspathOrFileSystem 方法):

private static InputStream  [More ...] findStreamInClasspathOrFileSystem(String name) throws FileNotFoundException 
{
  String path = null;
  if (name.startsWith("/")) {
    path = name.substring(1);
  }

  // - even though this may look like a regular file, it may be a path inside a jar in the CLASSPATH
  // - check for this first. This takes precedence over the file system.
  InputStream is = null;
  if (path != null) {
    is = IOUtils.class.getClassLoader().getResourceAsStream(path);

    // windows File.separator is \, but getting resources only works with /
    if (is == null) {
      is = IOUtils.class.getClassLoader().getResourceAsStream(path.replaceAll("\\\\", "/"));
    }
  }

  // if not found in the CLASSPATH, load from the file system
  if (is == null) is = new FileInputStream(name);
  return is;
}

更新:无论我是否将路径更改为:

  "c:/DP/lemma/models/english-left3words-distsim.tagger");
  "c:\\\\DP\\\\lemma\\\\models\\\\english-left3words-distsim.tagger");

它的行为仍然相同(在 eclipce 中有效,而不是在 cmd 中)

【问题讨论】:

  • 没有完整的代码是不可能知道的,但这行似乎是第一个嫌疑人:IOUtils.getInputStreamFromURLOrClasspathOrFileSystem。它无法正确识别这是一个文件并尝试从失败的类路径加载它
  • 我忘了告诉你,它在 Windows 7 64bit 上运行

标签: java


【解决方案1】:

您的代码似乎使用 ClassLoader 从 classath 加载资源。路径应采用以下形式:

com/yourcompany/yourapp/english-left3words-distsim.tagger

其中com.yourcompany.yourapp 是文件所在的包。

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29

编辑:

IOUtils.getInputStreamFromURLOrClasspathOrFileSystem() 的代码将两个格式错误的路径(c:\...c:/...)传递给 ClassLoader.getResourceAsStream(),并期望此方法简单地返回 null 而不是抛出异常,这是错误的。我会简单地决定我想在哪里加载资源 rom:类路径(因此使用ClassLosader.getResourceAsStream())或文件系统(因此使用new FileInputStream())。

【讨论】:

  • 谢谢。但我觉得我不太了解你。如果指定了绝对路径,为什么它看起来是 claspath?不会是路径字符串的语法有问题吗?
  • 可能是因为 edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem() 有问题。我会决定你真正想在哪里加载资源,然后直接从那里获取它:类路径或文件系统。
  • 这就是为什么在构造函数中接受 InputStream 而不是 String 更好的原因之一。经典错误..
  • 啊哈,谢谢!我不明白这一点。我不明白的是,为什么它在 Eclipse 中起作用:-|所以我必须把它放到系统路径..
猜你喜欢
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 2014-02-15
  • 1970-01-01
相关资源
最近更新 更多