5

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:

  • .idea
    • ...
  • out
    • ...
  • src
    • ListClass.java
    • MyClass.java
  • JavadocGenerator.iml
Farbod Salamat-Zadeh
  • 18,039
  • 16
  • 66
  • 118

1 Answers1

1

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.

heisbrandon
  • 1,111
  • 7
  • 6
  • Thanks. Running `javac -d . ListClass.java -cp C:/Program Files/Java/jdk1.8.0_66/lib/tools.jar` gives me `javac: file not found: ListClass.java`, so I changed it to `... src/ListClass.java ...` (see my updated question with the directory hierarchy). Now running this new command (with the `src/`) gives me the error `javac: invalid flag: Files/Java/jdk1.8.0_66/lib/tools.jar`. – Farbod Salamat-Zadeh Apr 25 '16 at 20:48
  • You are pretty close with what you have. I think you just need to put the path in quotes, and make sure you're executing javac and javadoc from the correct directories.. see my update. – heisbrandon Apr 25 '16 at 21:14
  • running the javac I get this `warning: com\sun\javadoc\RootDoc.class(com\sun\javadoc:RootDoc.class): major ver sion 51 is newer than 50, the highest major version supported by this compiler. It is recommended that the compiler be upgraded.` any idea about that ? – prime Aug 11 '17 at 12:36
  • command line work, but how can we do these things in IntelliJ IDE? I can't find any option to compile my doclet in the Generate JavaDoc dialog. – user1686407 Feb 01 '18 at 06:47