4

I am trying to compile a library I wrote, using javac and I am getting the error: package org.json does not exist. My program includes org.json.JSONArray and org.json.JSONException.

I have this package installed on my computer because I have successfully compiled android apps that import org.json libraries. I'm pretty sure all I have to do is specify a -classpath but I have been unable to find where these files live on my system (ubuntu 10.10 64-bit sun-java6).

Having been unable to find these on my own system I downloaded the org.json files from here, but I was unable to compile them individually because they were co-dependent on each other.

So I have a couple questions:

  1. Does anyone know where the org.json package lives from android sdk install?
  2. Where might I find a tutorial explaining these basic concepts regarding compiling, and javac.
Seanny123
  • 8,776
  • 13
  • 68
  • 124
dm03514
  • 54,664
  • 18
  • 108
  • 145
  • I *always* install everything Java-related on Un*x systems in the user's directory and I do this without needing root privileges (you cannot do it on Windows: on Windows you **must** have admin right to install Java). So, for me, *"java packages live somewhere in the user's directory"* :) – SyntaxT3rr0r Feb 26 '11 at 22:37

3 Answers3

3

Whatever external jars you need to compile with should be on the classpath when you compile. The most non-invasive way to do this is do add these items to the javac command line such as

javac -classpath /path/to/json.jar;. -g YourClass.java

or more likely if you use an IDE, add these jars to your referenced jars of the project in your IDE.

It usually isn't a good idea to pollute the global $CLASSPATH variable, as this then gets pulled in for everything you do with java, which may cause unintended conflicts.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
2

Wherever you like. What you need to do is examine your CLASSPATH variable, and make sure it includes the directory with your library.

Here's the first thing:

  $ echo $CLASSPATH

and you'll see your classpath as it is.

Now you need to find the jar file containing the org.json; consult the documentation, but it may be something as simple as json.jar. On most LINUX systems you can then just run

 $ locate json.jar

And you'll get a path name for the jarfile. Make sure that path is part of your CLASSPATH and you'll be in fat city.

Oh, and the "Getting started" tutorials at Sun Oracle are the easiest place to start.


Actually, having looked at the files, they may not be packaged as a jar file. In that case, you want to put them into your sources starting at some top directory (src in this example.)

   /src
       /org/json/ ... put the json files here
       ... put your files here

and when you compile, they'll all be included, which will resolve all the dependencies.

Again, the place to look for first steps is that tutorial.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
-1

use "java" command instead of "javac"