javajavadocdoclet

Trouble with generating custom javadoc; 'cannot find doclet'


I'm having a go at creating a custom Javadoc generator using Doclet, but I'm running into some issues.

I'm following the official documentation and initially had trouble with including the tools.jar file in my project, but I managed to fix this.

My issue now is that after running this command...

javadoc -doclet ListClass -docletpath .  MyClass.java

...I am getting the message...

javadoc: error - Cannot find doclet class ListClass

As I said, I've mostly been following the tutorials from the official documentation, but here is my code for reference.

ListClass.java:

import com.sun.javadoc.*;

public class ListClass {

    public static boolean start(RootDoc root) {
        ClassDoc[] classes = root.classes();
        for (int i = 0; i < classes.length; ++i) {
            System.out.println(classes[i]);
        }
        return true;
    }

}

And MyClass.java:

/**
 * Documentation for my class
 */
public class MyClass {

    public static void main(String[] args) {

    }

    /**
     * Documentation for my static void method
     *
     * @param param This is the parameter it takes in
     */
    public static void myStaticVoidMethod(String param) {

    }

}

So what I am asking is why I am getting the error I posted above. If someone was able to provide a more comprehensive guide of how Doclet works that would be great as well.


Note: I'm using IntelliJ IDE for my project. Here is my directory structure:


Solution

  • You need to compile your ListClass file. Something like:

    javac -d . ListClass.java -cp /path/to/tools.jar
    

    Doclet classes are part of the tools jar, so you'll need to include it as your compile-time dependency.

    Then your command

    javadoc -doclet ListClass -docletpath .  MyClass.java
    

    should work.

    edit

    For you project structure, if you're compiling from the root directory, make sure to reference your files through their subdirectories, and make sure any absolute windows paths are double-quoted:

    javac -d . ./src/ListClass.java -cp "C:/Program Files/Java/jdk1.8.0_66/lib/tools.jar"
    

    This would create a compiled ListClass file at the root of the project, and from there use your javadoc command:

    javadoc -doclet ListClass -docletpath .  ./src/MyClass.java
    

    It would be better to create a classes directory to place your compiled classes, as opposed to in the root of the project, but I'm just working with the structure you've provided. See the documentation for javac and javadoc for more info.