Link to home
Start Free TrialLog in
Avatar of farzanj
farzanjFlag for Canada

asked on

Java Json

I am trying to make a very basic parsing of this Json but I am getting strange error messages.

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);
    }
}

Open in new window


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

Open in new window


What am I doing wrong?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You're failing to use the same classpath for running as for compiling
Avatar of farzanj

ASKER

I am compiling using this command:

javac  -classpath ./google-gson-2.2.4/gson-2.2.4.jar json

Open in new window

And then running using
java  -classpath ./google-gson-2.2.4/gson-2.2.4.jar json

Open in new window


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.

Open in new window


BTW, I am using Linux.
Avatar of farzanj

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?
javac  -classpath ./google-gson-2.2.4/gson-2.2.4.jar json
                                            

Open in new window

should be
javac  -classpath ./google-gson-2.2.4/gson-2.2.4.jar json.java 

Open in new window

Avatar of farzanj

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.
Avatar of farzanj

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.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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of farzanj

ASKER

Ok, yes that works.  Reason?

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
Avatar of farzanj

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.
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
Avatar of farzanj

ASKER

Thanks