Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling .jar files from PHP - Stanford NLP - Could not find or load main java class

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
like image 664
Diego Vidal Avatar asked Sep 03 '16 10:09

Diego Vidal


1 Answers

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
like image 189
acm Avatar answered Oct 26 '22 01:10

acm