views:

112

answers:

4

I'm writing some clojure code, and I'm relying on Joda time for time handling. The problem is that I don't know what to import and the documentation isn't terribly clear about it. Now I know that somebody here can probably give me the correct answer in less than 5 seconds, but I'd rather know how to figure this one out on my own (aside from pouring through the docs until I find the correct answer).

If I were doing this in Python, I'd import the top-level package and use a combination of dir and help to figure out what I need to import. Is there any way to do this in clojure? Or are there any other kinds of java tools to figure this out? I'd prefer something that's command-line oriented.

+3  A: 

In the same way that dir and help will examine the documentation of packages in Python, useful information in Java can be gleaned from reading the Javadocs for a particular API. In the case of Joda Time, the web site has a direct link to the generated documentation, here:

http://joda-time.sourceforge.net/api-release/index.html (see the bottom left frame)

I can't think of anything off hand that is directly comparable to what you ask, since unless you ask the classloader for a particular class, the classloader won't load it, and you won't be able to find it.

David Grant
For the record, `dir` doesn't check documentation. It introspects the object itself. :-)
Jason Baker
@Jason Baker: thanks. :)
David Grant
+3  A: 

Normally I'd go with David Grant's answer, but if you can't find any JavaDoc (!), but you have a jar file, you can use the jar utility to list the files in the jar.

The directory the class files are in map directly to its package name. For example, jar -tf joda-time-1.6.jar | more lists DateTime.class as org/joda/time/DateTime.class; DateTime is in the org.joda.time package.

R. Bemrose
It's really not as much that I *can't* find it as much as it is that I don't *want* to find it. :-) If I'm already at the command-line or in emacs and I want to figure out what the import string is, it's much easier to do a `jar -tf` than it would be for me to switch to the browser and find the javadoc.
Jason Baker
+1  A: 

Bill Clementson has written a small piece of code that helps you view the javadoc documentation from the REPL.

Steen
A: 

http://dishevelled.net/Generating-Clojure-import-lines-using-SLIME.html Makes it really easy to look up imports. Personally I just use the clojure function find-classes defined there from the REPL.

Timothy Pratley