• Post Reply Bookmark Topic Watch Topic
  • New Topic

SImple way to add system class library  RSS feed

 
Marcus Kelvin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a simple way to specify a path in which to look for extra class files?

Note I don't mean $CLASSPATH, as far as I can. I'll try and illustrate with an example.

I downloaded the org.json jar from mvnrepo, and took the tree of class files out and put them into `/usr/local/lib/java`, so there's now, e.g. a `/usr/local/lib/java/org/json/JSONArray.class` file. I'm fooling around with this to confirm the API documentation for myself, so I create a `JsonTest.java` file in `/home/me/java`. I can compile this fine, but if I want to run



Of course it doesn't work -- it can't find the org.json stuff. So I set $CLASSPATH to `/usr/local/lib/java`, but this doesn't make any difference. So I try



Which gives me:



Obviously this is not going to work. It's including the arbitrary path in the package names. The only thing I can do is symlink `/usr/local/lib/java/org` into `/home/me/java`. Now everything is fine. But this is a total hassle, esp. if I'm using a VCS across several systems, etc -- I'm potentially going to end up having to include the actual org.json class files like in a jar, separately for every such project, when there's no reason each system can't simply have them installed wherever and let the local compiler and interpreter know where that is.

By comparison, for e.g., a C/C++ compiler I can set a non-standard, not-compiled in path using an environment variable, done. It does not then interpret that path as part of the `#include` spec.

Likewise, for a perl interpreter, I can specify a path for runtime libaries, and it does not expect that packages listed in source scripts be named `package usr::local::lib::perl::foo::bar` -- but it will look for `package foo::bar` in `/usr/local/lib/perl` if that is part of the runtime include path. Same thing with the system linker for native binaries.

What have I misunderstood here? Is this more or less an anti-method with Java, as in, "Sorry, won't let you do that!"?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

You need to add the jar file name, not just its location. Java will then know to look inside that jar file for classes.
 
Marcus Kelvin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

You need to add the jar file name, not just its location. Java will then know to look inside that jar file for classes.


There is no jar file. There is just a directory of class files I took out of the jar. And are you saying if it were I jar, I need to add the entire path? Then I would have to do it for every jar!

Incidentally, the behavior I'm observing seems contra to this post: http://www.coderanch.com/t/660138/Wiki/Set-Classpath
Which states unequivocably:

If you are trying to locate classes in a directory, then specify the path up to but not including the name of the package the classes are in. (If the classes are in a package called my.package and they are located in a directory called C:\myclasses\here\my\package, you would set the classpath to be CLASSPATH=C:\myclasses\here)


If this were the case, then a CLASSPATH of `/usr/loca/lib/java` should allow me to use stuff in package `org.json` if `/usr/local/lib/java/org/json` exists (it does). But that doesn't work. It then seem to expect me to `import usr.local.lib.java.org.json`.

So I'm pretty confused at this point.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you pull out .class files from a jar file? That's contradictory to the intended use of jar files. Normally, you would put all jar files on which your program is dependent in a single lib directory and include that lib directory in your classpath with a * wildcard, as decribed here: http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath
 
Marcus Kelvin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thankfully this was my mistake. I was overriding $CLASSPATH by using `-cp .` as well, since setting $CLASSPATH had overridden the default `.` directory.

Confirmed that with:



Notice the `:.` Phew. Thought the planet was a crazier place than it should be for a while...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!