44

How do I write links into javadocs?

Currently, I have something like:

{@link java.lang.Math#sqrt(double) Math.sqrt}

to produce the text Math.sqrt that should link to the java.lang.Math.sqrt(double) API, however, all it does is produce the text, no link.

mtk
  • 13,221
  • 16
  • 72
  • 112
masher
  • 3,814
  • 4
  • 31
  • 35

4 Answers4

21

My answer is very much provided by Eddie, but his exact code doesn't work for me (or at least when using the version of javadoc that comes with Java 1.6)

If I do:

    javadoc -linkoffline http://java.sun.com/javase/6/docs/api/ 
                         http://java.sun.com/javase/6/docs/api/package-list
            -public FileName.java

then javadoc complains:

    javadoc: warning - Error fetching URL: 
    http://java.sun.com/javase/6/docs/api/package-list/package-list

If, on the other hand, I do:

    javadoc -linkoffline http://java.sun.com/javase/6/docs/api/ 
                         http://java.sun.com/javase/6/docs/api/ 
            -public FileName.java

Then it works, and my links are populated as I want them to be.

Additionally, my link isn't malformed. The text {@link java.lang.Math#sqrt(double) Math.sqrt} produces the link text Math.sqrt instead of the default Math.sqrt(double).

masher
  • 3,814
  • 4
  • 31
  • 35
  • As for the first variant (when you included *package-list* in the URL), the [javadoc documentation page](http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javadoc.html#linkoffline) page says for the parameter *packagelistLoc* (quote): **Do not include the *package-list* filename.** So that pretty much explains it all. – informatik01 Apr 22 '13 at 14:56
19

To get a link to something external to your code, you need use the -linkoffline option

where the -linkoffline option has the format something like this (artificially wrapped):

-linkoffline http://java.sun.com/javase/6/docs/api/
             http://java.sun.com/javase/6/docs/api/

This tells the JavaDoc tool where to find the link to the JavaDoc and for what packages to use that link. From the 2nd URL, it will append "package-list" to load the actual URL:

http://java.sun.com/javase/6/docs/api/package-list

which you can verify by loading it in a browser does contain the list of packages documented at that JavaDoc URL. This tells the JavaDoc tool that any @link references to anything in one of those packages should link to the provided URL.

Eddie
  • 53,828
  • 22
  • 125
  • 145
  • Your exact answer doesn't work for me, but you gave me what I needed. – masher Mar 31 '09 at 04:54
  • 2
    The link isn't malformed -- you can provide a string of text to be used as the link label as the second argument to @link. – mipadi Mar 31 '09 at 05:57
  • @mipadi: Oh! In all my years of Java programming, I've never seen that. Thanks for letting me know. I'll correct my answer. – Eddie Mar 31 '09 at 16:42
0

I believe for what you're trying to do, you can do this:

@see java.lang.Math#sqrt(double)

It doesn't give you the alternate text, but it gives you the link.

You can also write a more generic link like using standard html:

<a href="http://www.google.com">Google</a>
MiguelMunoz
  • 4,548
  • 3
  • 34
  • 51
-1

This document might be of assistance, remember that for @link you need to use the URL for the doc you are linking to.

Martin Spamer
  • 5,437
  • 28
  • 44
Sebastian Oliva
  • 335
  • 3
  • 7