farzanj
asked on
Java Json
I am trying to make a very basic parsing of this Json but I am getting strange error messages.
There is no compile time error message.
On run time this is what I get.
What am I doing wrong?
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.List;
public class json
{
public static void main(String[] args)
{
String line = "{\"handlerService\":\"ORIGINATION_XX\",\"type\":\"DLV\",\"srcAddress\":{\"pin\":\"D4AAAABC\",\"guid\":\"XXY_P2P\",\"suid\":null,\"endPointType\":\"PIN\",\"serverMessage\":false},\"destinationCount\":1,\"dstAddresses\":{\"pin\":\"da888111\",\"guid\":null,\"suid\":null,\"endPointType\":\"PIN\",\"serverMessage\":false},\"source\":\"UNKNOWN\",\"userAgent\":null,\"nextHop\":\"\",\"msgStatus\":\"RESPONSE_SENT\",\"msgId\":\"14419ASF93\",\"callId\":\"1basfdsf925771d032aa3e81895037@10.132.22.129\",\"country\":\"UNKNOWN\",\"logVersion\":\"1.0\",\"sipStatus\":200,\"gmeError\":0,\"deliveryInfoRequired\":0,\"payloadInfo\":{\"contentLength\":0,\"appType\":\"0\",\"appSubType\":\"0\",\"contentId\":\"ABB_IM\"}}";
System.out.println(line);
JsonElement jelement = new JsonParser().parse(line);
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("srcAddress");
String result = jobject.get("pin").toString();
System.out.println(result);
}
}
There is no compile time error message.
On run time this is what I get.
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/JsonParser
at json.main(json.java:15)
Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonParser
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
What am I doing wrong?
You're failing to use the same classpath for running as for compiling
ASKER
I am compiling using this command:
And get the error message like:
BTW, I am using Linux.
javac -classpath ./google-gson-2.2.4/gson-2.2.4.jar json
And then running usingjava -classpath ./google-gson-2.2.4/gson-2.2.4.jar json
And get the error message like:
Exception in thread "main" java.lang.NoClassDefFoundError: json
Caused by: java.lang.ClassNotFoundException: json
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: json. Program will exit.
BTW, I am using Linux.
ASKER
My follow up questions would be:
1. How do I deploy it?
2. Can I make these libraries a part of object code so that I don't need to mention it using -classlib syntax?
3. Typically in C++ in order to deploy I either provide a Makefile sometimes configure or even RPM. What is the standard way to deploy a Java application? Are there shared libraries sometimes?
1. How do I deploy it?
2. Can I make these libraries a part of object code so that I don't need to mention it using -classlib syntax?
3. Typically in C++ in order to deploy I either provide a Makefile sometimes configure or even RPM. What is the standard way to deploy a Java application? Are there shared libraries sometimes?
should bejavac -classpath ./google-gson-2.2.4/gson-2.2.4.jar json
javac -classpath ./google-gson-2.2.4/gson-2.2.4.jar json.java
ASKER
Yes, it is just a typo in my question. I have this.
It is compiling fine. If I had missed .java, it would complain that the file is not found.
In any case, when I run it, I still get error messages as shown above.
It is compiling fine. If I had missed .java, it would complain that the file is not found.
In any case, when I run it, I still get error messages as shown above.
ASKER
Just copied from the console:
$ javac -classpath ./google-gson-2.2.4/gson-2 .2.4.jar json.java
$ java -cp google-gson-2.2.4/gson-2.2 .4.jar json
Exception in thread "main" java.lang.NoClassDefFoundE rror: json
Caused by: java.lang.ClassNotFoundExc eption: json
at java.net.URLClassLoader$1. run(URLCla ssLoader.j ava:202)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:19 0)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:306)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :301)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:247)
Could not find the main class: json. Program will exit.
$ javac -classpath ./google-gson-2.2.4/gson-2
$ java -cp google-gson-2.2.4/gson-2.2
Exception in thread "main" java.lang.NoClassDefFoundE
Caused by: java.lang.ClassNotFoundExc
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
Could not find the main class: json. Program will exit.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, yes that works. Reason?
Also please address the questions in 39351008
Also please address the questions in 39351008
The reason is that you missed a path (the current directory, which contains json.class [incidentally classnames in Java should begin upper case]) out of the classpath
To answer your other questions, i'd need to know the purpose of your code. Currently it looks like a fragment of something else
To answer your other questions, i'd need to know the purpose of your code. Currently it looks like a fragment of something else
ASKER
Right now I am writing for Hadoop that would run on Linux server. I haven't used Java for a long time but I have to get back to it because Hadoop is Java oriented. But in general as well, I need to know the best practices of delivering Java code for, say, servers and for web work or even desktop.
For Hadoop I think the answer is creating executable Jar but I need the syntax for that as well.
For Hadoop I think the answer is creating executable Jar but I need the syntax for that as well.
An executable jar is a good start - it's the basis of many different scenarios
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
Bear in mind that, if want to use the resulting jar as an executable jar (no classpath specified by the 'user') you need to use the Class-Path attribute in the manifest for all dependencies
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
Bear in mind that, if want to use the resulting jar as an executable jar (no classpath specified by the 'user') you need to use the Class-Path attribute in the manifest for all dependencies
ASKER
Thanks