问题
I am trying to run a java program that uses protobuf.jar, but I keep getting this error.
I have set my classpath variable in linux so that:
CLASSPATH=/home/.../src/PlaceServer.class:/home/.../src/protobuf.jar:/home/.../src
export CLASSPATH
But then when I run my program in the command line after reading in the jar.
java PServer
I get this:
java.lang.NoClassDefFoundError: com/google/protobuf/MessageOrBuilder
However when I run another program that also reads the same jar, this one called BServer
java BServer
It works fine, and correctly as I want it. I even tried running under these commands instead
java -cp .protobuf.jar PServer
And it still did not work for PServer.
However, if I run the same programs on my Macbook from the command line (also in Eclipse in either OS) I do not get this Exception and it all works fine.
Thanks for your help!
回答1:
There are a couple of things to check to get rid of this error:
- Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.
- The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution
When you run the application in Eclipse, the IDE resolves this by using the .classpath file inside the project folder. When you build an application (create the jar), you could accidentally omit this class, or change its location.
What you need to do is to first open the jar, and make sure that the class in question is in fact inside the jar, in the same path. Then, go through the list above.
来源:https://stackoverflow.com/questions/26834465/classpath-set-but-java-lang-noclassdeffounderror-com-google-protobuf-messageorbu