Error running and compiling ParserDemo2 - java

I'm trying to programatically generate dependency trees using the Stanford Parser, following the guide to running ParserDemo found at http://nlp.stanford.edu/software/parser-faq.shtml
I run the line:
java -mx200m -cp "stanford-parser.jar:." ParserDemo2 englishPCFG.ser.gz testsent.txt
which results in
Could not find or load main class ParserDemo2
This makes sense, I need to compile, so I run
javac -cp "stanford-parser.jar:." ParserDemo2.java
which generates a .class file.
However, when I run it using the above line, I get:
java -mx200m -cp "stanford-parser.jar:." ParserDemo2 englishPCFG.ser.gz testsent.txt
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at edu.stanford.nlp.parser.common.ParserGrammar.<clinit>(ParserGrammar.java:46)
at ParserDemo2.main(ParserDemo2.java:25)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
What am I doing wrong? Something pretty basic?

stanford-parser.jar, has dependency on other jars. You need to add them to classpath.

Related

Running java program using packages in multiple folders

I'm trying to run a program with the following structure:
+src
+gui
-XL.java
-moreFiles.java
+menu
-guiFiles.java
+util
-utilFiles.java
+extra
-extraFiles.java
I'm trying to compile the code by calling
javac gui/XL.java
Which succeeds.
When I try running the code with
java gui.XL
I get the following error message:
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
at gui.XL.<init>(XL.java:25)
at gui.XL.main(XL.java:58)
Caused by: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
... 2 more
Caused by: java.lang.ClassNotFoundException: java.lang.invoke.StringConcatFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
I guess I have some problems with my classpath but I have no clue about how to fix it. Does anyone have any suggestions?
My problem had nothing to do with classpaths or that I wasn't using an IDE. My problem came from the fact that I was changing the 'java' command to run my java-openjdk-8 but my compiler still ran with java-openjdk-9. The solution was therefore
sudo update-alternatives --config javac #Change to java 8
sudo update-alternatives --config java #Change to java 8

java.lang.ClassNotFoundException: com.github.lwhite1.tablesaw.api.Table error while running Spark-submit using Eclipse

I am executing the java archive on Spark using spark-submit in Ubuntu. The command is given below. This JAR file was build using Maven Package. The dependencies are specified in pom.xml file.
]$ spark-submit --class HighScore.Driver --master local[*] JarfilePath/Levelwise_PCFS-0.0.1-SNAPSHOT.jar InputFilePath/K9_Site1.csv 1000.
I am getting following error even when packageName.className (HighScore.Driver) is specified in the command.
Here is the error message.
Exception in thread "main" java.lang.NoClassDefFoundError: com/github/lwhite1/tablesaw/api/Table
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:727)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: com.github.lwhite1.tablesaw.api.Table
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
com/github/lwhite1/tablesaw/api/Table dependency was also specified in pom.xml file. But still it throws the exception.
Can some one help me in rectifying this error.
Remember that the classloader (specifically java.net.URLClassLoader) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath.
NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.
For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class.
Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.
Please refer this post for more details. It would be helpful.

Running java file with jar and text file input

I am trying to run a program on the command line that uses the JavaMail API. This program also reads in a message from a text file. However, I'm getting this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Originally I used java -cp /path/to/mail.jar mailTest < message.txt
I configured the classpath for the jar successfully (as far as I know). Can anyone give me some pointers? :/
Your classpath is definitely wrong. The classpath needs to include both the javax.mail.jar file and the classes for your application. Exactly where is the JavaMail jar file, exactly where are your application classes, and exactly what java command line did you use? If your application classes are in the current directory, you need something like
java -cp /path/to/javax.mail.jar:. mailTest
Actually if you look at the error message:
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
That's probably because your jar depends on another jar (probably mail-x.y.z.jar) and you did not include it on your classpath when trying to run your jar.
Either include the missing jar(s) on the command line, see this post, or add them to your manifest file, see the doc

MongoDB with java Exception in thread "main" java.lang.NoClassDefFoundError: org/bson/conversions/Bson

When I execute mycode from intelliJ Idea it works fine but when I run the jar on my server it throws the below error. I am using 'org.mongodb:mongodb-driver:3.4.1' version.
Exception in thread "main" java.lang.NoClassDefFoundError: org/bson/conversions/Bson
at com.myapp.server.mongo.MongoDbHandler.prepareMongoDb(MongoDbHandler.java:68)
at com.myapp.server.mongo.MongoDbHandler.<init>(MongoDbHandler.java:22)
at com.myapp.server.Client.main(Client.java:167)
Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
I know its an old thread and its just for those who have the similar issues. Few days back I was also facing the same issue with same error trace while running the jar by command line.After rechecking the code found that
It was not a problem of the jar version but its problem of the packaging of the
jar
After packing the jar with all proper dependencies will solve this issue.
I have used
maven-assembly-plugin
for packaging and its works.
.
If I had to guess you need to get yourself whatever BSON library you're using; I wouldn't say that Mongo is your problem.
key line here is:
"Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson"
So it's saying that it's not finding that particular class, I would look in IntelliJ and see what BSON library it is, then put it in your build directory (or java lib) on your server.

Properly Linking Library

I am trying to run a jar file that is using pi4j.
sudo java -classpath /opt/pi4j/lib/pi4j-core.jar -jar Test.jar test.Main
Problem is I keep getting this error
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
at test.Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I don't see why the link isn't being made properly.
I have never done it in command line. But this worked for me:
in IntelliJ, I added the pi4J jar files to my project as modules. I then made an artifact and got IntelliJ to create a jar file for me (with all the dependencies including pi4J). Then I transferred the jar file to the pi and it ran fine.

Categories

Resources