[英]GSON “NoClassDefFoundError com/google/gson/Gson”
I'm trying to get gson to work on a java project, but get the above error every time I run it.我试图让 gson 在一个 java 项目上工作,但每次运行它时都会出现上述错误。 I am not using any flash IDEs, just vim and every question I have seen related to my question is to do with eclipse and their deployment settings.
我没有使用任何 flash IDE,只是使用 vim,我看到的与我的问题相关的每个问题都与 eclipse 及其部署设置有关。 I hope someone can spot something wrong in my tactics.
我希望有人能在我的策略中发现错误。
Note: I have the gson jar file renamed to gson.jar注意:我将 gson jar 文件重命名为 gson.jar
To compile my code: javac myProgram.java -cp gson.jar
编译我的代码:
javac myProgram.java -cp gson.jar
and to run my code: java myProgram -cp gson.jar
并运行我的代码:
java myProgram -cp gson.jar
The program will compile fine, but it will throw an error as soon as I attempt to initiliaze a Gson
object.该程序可以正常编译,但是一旦我尝试初始化
Gson
对象,它就会抛出错误。
Full error: Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
完整错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
I believe you have the javac and java argument order mixed up.我相信你把 javac 和 java 参数顺序搞混了。 The syntax is:
语法是:
javac <options> <source files>
and和
java [-options] class [args...]
So it should be something like所以它应该是这样的
javac -cp gson.jar myProgram.java
Same goes for the java
command. java
命令也是如此。
Include the present directory location of myProgram or compiled java class.包括myProgram或编译的 java 类的当前目录位置。 So run;
所以跑;
java -cp gson.jar:. myProgram
The dot assumes you are running myProgram from the same dir location it is place.该点假设您正在从它所在的同一目录位置运行 myProgram。 Else use "full path to myProgram".
否则使用“myProgram 的完整路径”。 Aleksander Stojadinovic is also right, the order of things matters with the java runtime environment!
Aleksander Stojadinovic也是对的,Java 运行时环境的顺序很重要!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.