0

Why the following iniciation works in eclipse:

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

but in command line it throws:

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 corresponding with MaxentTagger iniciation..

Is there a different way to declare windows path, which works in both, eclipse and cmd?

update (the findStreamInClasspathOrFileSystem method):

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;
}

update: no matter if i change the path to:

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

its behaviour is still the same (works in eclipce, not in cmd)

gaffcz
  • 3,469
  • 14
  • 68
  • 108
  • Possible duplicate of : http://stackoverflow.com/questions/2030434/eclipse-no-java-jre-jdk-no-virtual-machine – Shuhail Kadavath Aug 06 '13 at 06:51
  • It is impossible to know without complete code but this line seems the first suspect: `IOUtils.getInputStreamFromURLOrClasspathOrFileSystem`. It does not correctly recognize this is a file and tries to load it from classpath which fails – c.s. Aug 06 '13 at 06:59
  • And i forgot to tell you, it's running in Windows 7 64bit – gaffcz Aug 06 '13 at 07:05

1 Answers1

3

Your code seems to load a resource from the classath, using the ClassLoader. The path should take the following form:

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

where com.yourcompany.yourapp is the package where the file resides.

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

EDIT:

The code of IOUtils.getInputStreamFromURLOrClasspathOrFileSystem() passes two badly formatted paths (c:\... and c:/...) to ClassLoader.getResourceAsStream(), and expects this method to simply return null instead of throwing an exception, which is wrong. I would simply decide where I want to load the resource rom: either the classpath (and thus use ClassLosader.getResourceAsStream()) or the file system (and thus use new FileInputStream()).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you. But I think I don't understand you well. Why it looks to claspath, if the absolute path is specified? Couldn't be the problem in the sysntax of path string? – gaffcz Aug 06 '13 at 06:57
  • Probably because edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem() is buggy. I would decide where you really want to load the resource, and directly get it from there: either the classpath, or the file system. – JB Nizet Aug 06 '13 at 07:01
  • This is one reason of why it's better to accept an InputStream instead of a String in a constructor. Classic mistake.. – Patrick Aug 06 '13 at 07:15
  • Aha, thank you! I've undestood it that. What I don't understand is, why it works in eclipse :-| So I have to, place it to system path.. – gaffcz Aug 06 '13 at 07:16