11

I've got a project that is using this package agentile/PHP-Stanford-NLP (PHP interface to Stanford NLP Tools (POS Tagger, NER, Parser) which calls a few .jar files. Everything is working ok on localhost (MAMP) but when I deployed it to laravel forge it is not working anymore. I installed JRE/JDK, Oracle JDK, Oracle JDK 8 in my server.

This is the piece of code I use to call the java files:

$parser = new \StanfordNLP\Parser(
        public_path().'/stanford-parser.jar',
        public_path().'/stanford-parser-3.4.1-models.jar'
);
$parser = $parser->parseSentence($text);

This is the piece of code where the error comes from:

$parser = $this->lexicalized_parser ? 'edu/stanford/nlp/models/lexparser/englishFactored.ser.gz' : 'edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz';
$osSeparator = $this->php_os == 'windows' ? ';' : ':';
$cmd = $this->getJavaPath()
     . " $options -cp \""
     . $this->getJar()
     . $osSeparator
     . $this->getModelsJar()
     . '" edu.stanford.nlp.parser.lexparser.LexicalizedParser -encoding UTF-8 -outputFormat "'
     . $this->getOutputFormat()
     . "\" "
     . $parser
     . " "
     . $tmpfname;
$process = proc_open($cmd, $descriptorspec, $pipes, dirname($this->getJar()));

https://github.com/agentile/PHP-Stanford-NLP/blob/51f99f1aaa1c3d5822fe634346b2b4b33a7a6223/src/StanfordNLP/Parser.php#L90

This is the error:

Error: Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser

EDITED:

This is the $cmd output from localhost:

java -mx300m -classpath */Applications/MAMP/htdocs/mydomainname/public/lib/slf4j-api.jar:/Applications/MAMP/htdocs/mydomainname/public/lib/slf4j-simple.jar:/Applications/MAMP/htdocs/mydomainname/public/stanford-parser.jar:/Applications/MAMP/htdocs/mydomainname/public/stanford-parser-3.4.1-models.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser -encoding UTF-8 -outputFormat wordsAndTags,penn,typedDependencies edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz /private/tmp/phpnlpparserC7ptSf

This is the $cmd output from production:

java -mx300m -classpath */home/forge/mydomainname.com/public/lib/slf4j-api.jar:/home/forge/mydomainname.com/public/lib/slf4j-simple.jar:/home/forge/mydomainname.com/public/stanford-parser.jar:/home/forge/mydomainname.com/public/stanford-parser-3.4.1-models.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser -encoding UTF-8 -outputFormat wordsAndTags,penn,typedDependencies edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz /tmp/phpnlpparserRdsoE5
Diego Vidal
  • 1,022
  • 12
  • 21
  • Updating to the latest version (as of writing [3.6.0](http://nlp.stanford.edu/software/lex-parser.shtml#Download)) may very well fix the issue, also see possible duplicate [Stanford-NLP: Could not find main class error](http://stackoverflow.com/q/27955569/934739). – Gerard Roche Sep 05 '16 at 23:15
  • can you echo the command this is generating? i would start there. likely you are missing something in your path. – Sari Yono Sep 06 '16 at 11:43
  • What walue have `cmd` when it invoked at you machine and at server? – talex Sep 07 '16 at 15:01
  • Thanks for your reply, I tried using all different versions (3.4.1, 3.5.1, 3.5.2, 3.6.0), I will edit my question including the $cmd when I get home – Diego Vidal Sep 07 '16 at 16:27
  • Could you check if the file `stanford-parser.jar` is actually in the given path in your server (i.e. `ls /home/forge/mydomainname.com/public/stanford-parser.jar`)? If it is actually there, could you replace it by the one that you have in your local computer and share the results? – acm Sep 10 '16 at 16:28
  • The paths are correct. I have already changed the files and the paths a lot of times targeting different folders, even with absolutes paths. That is not the problem. I believe the problem is related with the production server and the way I have to call the .jar files from there. – Diego Vidal Sep 11 '16 at 18:13
  • Well actually, your error is pretty clear. It cannot find `edu.stanford.nlp.parser.lexparser.LexicalizedParser` in the provided classpath. This specific class is inside `stanford-parser-X.X.X.jar`. The must likely cause for this error is that you are not providing a valid path for `stanford-parser-X.X.X.jar`. That's why I'm asking you to check only this specific file existence, if it exists write in your console: `java -classpath /home/forge/mydomainname.com/public/stanford-parser.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser` and please share the results. – acm Sep 11 '16 at 22:32
  • This is the error I see from the console (same error that I see in the browser): forge@jubilant-firefly:~$ java -classpath /home/forge/mydomainname.com/public/stanford-parser.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser Error: Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser forge@jubilant-firefly:~$ – Diego Vidal Sep 12 '16 at 12:57
  • 1
    If your are using version 3.6.0, could you run the following in your terminal: `wget http://central.maven.org/maven2/edu/stanford/nlp/stanford-parser/3.6.0/stanford-parser-3.6.0.jar && mv stanford-parser-3.6.0.jar /home/forge/mydomainname.com/public/stanford-parser.jar`. It will replace your current `stanford-parser.jar` for the one in Maven Central. After that, please try again the command in my previous comment and share the error. It should be different. – acm Sep 12 '16 at 14:12
  • After following your step as you said the file was replaced and it shows a different comment: Basic usage (see Javadoc for more): java edu.stanford.nlp.parser.lexparser.LexicalizedParser parserFileOrUrl filename* – Diego Vidal Sep 12 '16 at 16:35
  • Well, your problem be solved then. Can you check if it is really solved? – acm Sep 12 '16 at 16:44
  • I checked. First of all thank you because it seems that you are giving me a good solution. The new error is: I edited the question to show the error. – Diego Vidal Sep 12 '16 at 17:02
  • Sir, I repeated the same process you said in the previous comment for stanford-parser-3.4.1-models.jar and it is working now. Thank you very much for your help. Really! Please can you open an answer showing your solution so I can give you the bounty? – Diego Vidal Sep 12 '16 at 17:11
  • I need to also replace stanford-postagger.jar. Do you know where to find it? I tried in Maven Central with no success. – Diego Vidal Sep 12 '16 at 17:34
  • You are welcome. Have you tried this one http://nlp.stanford.edu/software/stanford-postagger-2015-12-09.zip? – acm Sep 12 '16 at 17:42
  • Yes, all versions, but the thing is that I have the same problem as before. I think I need to replace it as we did before. Do you know any other repository similar to maven central that has this file? – Diego Vidal Sep 12 '16 at 17:44
  • Not that I know. But if it is running in your local computer it should be running in your server. Just replace the one in your server with your local version. Be sure the user that runs the `java` command has read permission for this file. – acm Sep 12 '16 at 17:47
  • Sorry for asking again sir, but the error is the same I was facing before, only that this time I try to open stanford-postagger.jar. Error: Could not find or load main class edu.stanford.nlp.tagger.maxent.MaxentTagger If you have a possible solution to this problem I will appreciate it. – Diego Vidal Sep 12 '16 at 17:53
  • 1
    No problem. Just download the like I post in my comment before. Unzip the file and replace `stanford-postagger.jar` as you did before. You can do that with the following command: `wget http://nlp.stanford.edu/software/stanford-postagger-2015-12-09.zip && unzip stanford-postagger-2015-12-09.zip && mv stanford-postagger-2015-12-09/stanford-postagger-3.6.0.jar path/stanford-postagger.jar` where path is the path you are using in your `-classpath` option. – acm Sep 12 '16 at 18:05
  • Amazing! Thank you! Please open an answer so I can give you the bounty – Diego Vidal Sep 12 '16 at 18:22
  • You are welcome. I'm glad it helped. – acm Sep 12 '16 at 18:29

2 Answers2

2

There can be two issues

1) Please check if the paths /home/forge/mydomainname.com/public/lib /home/forge/mydomainname.com/public/ and corresponding jar files are present. 2) Check the access for all these files (is the access on these files same as your MAMP?)?

Declare
  • 76
  • 7
  • The paths are correct. When the paths are not correct the system displays a different error message, something like: .jar file path could not be found... – Diego Vidal Sep 11 '16 at 18:14
  • How about access? – Declare Sep 11 '16 at 19:51
  • @DiegoVidal when you write an incorrect path in the `-classpath` option it does **not** display a message indicating it cannot be found. – acm Sep 11 '16 at 22:35
  • @DiegoVidal as @acm mentioned you will not get any error message if your path is incorrect. Please verify the points I mentioned, it can be a missing file or the files in your prod has incorrect access. Try running `ls -l` to compare with your MAMP files. – Declare Sep 11 '16 at 23:49
  • This is the error I from the console (same error that I see in the browser): forge@jubilant-firefly:~$ java -classpath /home/forge/mydomainname.com/public/stanford-parser.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser Error: Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser forge@jubilant-firefly:~$ – Diego Vidal Sep 12 '16 at 12:58
  • can you just share the output of the command `ls -l /home/forge/mydomainname.com/public/stanford-parser.jar`? – Declare Sep 12 '16 at 13:06
  • Output: -rw-rw-r-- 1 forge forge 3645732 Sep 5 2014 /home/forge/mydomainname.com/public/stanford-parser.jar – Diego Vidal Sep 12 '16 at 16:37
2

The error message you posted:

Error: Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser

indicates that your class could be found by the java command. Which means your class is not in the classpath.

The class edu.stanford.nlp.parser.lexparser.LexicalizedParser should be inside stanford-parser.jar which you are manually including in the classpath.

In this scenario (since you said in the comments that the file actually exists) there are two main reasons that could cause the problem:

  • You don't have read permission for this file.

  • Your file is somehow corrupted or it is not the same one you are using in your local environment (it does not contain the referred class).

The first cause is unlikely if you uploaded the files with the same user with which you are running the process, in any case it is easy to check and fix.

The second cause can be solved by downloading a clean version and replacing the current one. You can download the new version from Maven Central and replace the one in your server using the following command:

wget http://central.maven.org/maven2/edu/stanford/nlp/stanford-pa‌​rser/3.6.0/stanford-‌​parser-3.6.0.jar && mv stanford-parser-3.6.0.jar /home/forge/mydomainname.com/public/stanford-parser.jar
acm
  • 2,086
  • 3
  • 16
  • 34