Question

I downloaded a .jar file from JSON lib: http://sourceforge.net/projects/json-lib/files/

Then I went to netbeans and imported the file into my library, now how do I used JSONobject in my program?

I think I need to do some sort of import statement, but Im not sure

Thanks in advance, Im a noob

Was it helpful?

Solution

If your try to use a Class from the library your IDE should suggest the correct import statement. (May have to save first to see the suggestion)

Alternatively you can manually:

import some.lib.Class;

Edit: I've downloaded your linked jar, added it to a new project's Build Path and the following code gives no errors:

package test;

import net.sf.json.JSONObject;

public class TestJSON
{
    JSONObject jsonObj;

    public TestJSON()
    {
        jsonObj = new JSONObject();
    }
}

Can you give us some erors, warnings or stacktraces please.

See this question for how to go about adding jars to your build path in Netbeans: How to add a JAR in NetBeans

OTHER TIPS

You can find a simple yet detailed explanation of imports here. Short answer, Yes, you need the import directive.

I believe you will find the imports you seek with

 import net.sf.json.*; // e.g. JSONObject

In the future, you can always run jar tvvf json-lib-2.4-jdk15.jar and get the contents yourself. Further, in addition to the jar utility, you can open JAR file with any application that can read zip files (because jars are zip files).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top