22

I have generated javadoc with javadoc maven plugin 3.0.1 and Java 9. However, when I use the new Search capability and pick a class, it redirects to "File not found"...

There is undefined in the url (e.g. "../target/site/apidocs/undefined/com/mycompany/MyClass.html"), which if removed, loads the page correctly.

Could you please help me with the right configuration to generate java doc (get rid of this undefined), so the search capability load the html page fine?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>${version.maven-javadoc-plugin}</version>
        <executions>
            <execution>
                <id>javadoc</id>
                <goals>
                    <goal>javadoc</goal>
                </goals>
                <phase>prepare-package</phase>
                <configuration>
                    <doclint>none</doclint>
                    <dependencySourceIncludes>
                       <dependencySourceInclude>com.some:some</dependencySourceInclude>
                    </dependencySourceIncludes>
                    <doctitle>Title - ${project.version}</doctitle>
                    <includeDependencySources>true</includeDependencySources>
                    <windowtitle>Title</windowtitle>
                </configuration>
            </execution>
        </executions>

Radoslav Ivanov
  • 970
  • 8
  • 23

6 Answers6

20

Got this working with the help of the following option in the java doc maven plugin configuration

<additionalJOption>--no-module-directories</additionalJOption>
Radoslav Ivanov
  • 970
  • 8
  • 23
  • Do you know if this is a bug of javadoc, for we should be allowed to have an unnamed module ? – Kineolyan Jan 22 '19 at 09:31
  • Can't say, I guess the default behavior is expecting the project to have module structure since the new jigsaw feature. May be the javadoc executable (or maven plugin) should detect the structure and be somewhat consistent/backward compatible. – Radoslav Ivanov Jan 29 '19 at 01:42
  • If you only use Eclipse, you can add it into Extra Javadoc options like: "--no-module-directories" – www Mar 07 '19 at 15:50
  • 1
    I can confirm this works, but this option went away in JDK13... https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8215580 – Arnout Engelen Jul 06 '21 at 14:19
  • you should not need that in jdk13 as this was fixed in jdk12 (as somebody mentioned in one of the answers here). – Radoslav Ivanov Jul 07 '21 at 09:21
6

This is a bit of a hack, but as @Martin Goik has mentioned in his answer, specifying --no-module-directories breaks any external links to standard classes due to missing module names. As the OP has mentioned in a comment, getURLPrefix(ui) of search.js is what generates the module names. So after some digging around, I found that the problem can be fixed by appending this to the end of search.js:

getURLPrefix = function(ui) {
    return "";
};

which essentially overwrites the definition of getURLPrefix to return an empty string no matter what. As it only needs to be appended to the end of a file, it should be easy enough to automate with any build tool by code or a command.

For example, if you're using Gradle, the following can be added to build.gradle:

// Needed to fix Javadoc search
// See comments below
final JAVADOC_FIX_SEARCH_STR = '\n\n' +
'getURLPrefix = function(ui) {\n' +
'    return \'\';\n' +
'};\n'

tasks.withType(Javadoc) {
    // Link to external docs
    options.with {
        // Example: Java 11 API documentation
        links 'https://docs.oracle.com/en/java/javase/11/docs/api/'
    }

    doLast {
        // Append the fix to the file
        def searchScript = new File(destinationDir.getAbsolutePath() + '/search.js')
        searchScript.append JAVADOC_FIX_SEARCH_STR
    }
}
Tyler Tian
  • 597
  • 1
  • 6
  • 22
  • Do you happen to know how to accomplish this with pom+maven? – Joris Kinable May 29 '20 at 06:27
  • 1
    @JorisKinable Sorry, I've never actually used Maven directly before, so I'm not really sure how you would append text to a file. Upon doing some research I found https://stackoverflow.com/questions/14896059/append-value-in-a-file-using-maven and https://github.com/olivierlemasle/plaintext-maven-plugin, which may be helpful, but that's all I know. – Tyler Tian May 29 '20 at 07:15
5

Each .html file generated by Javadoc contains the following variable definition:

var useModuleDirectories = true;

which affects the behavior of the getURLPrefix() function in search.js:

function getURLPrefix(ui) {
    var urlPrefix="";
    if (useModuleDirectories) {
        ...
    }
    return urlPrefix;
}


Therefore, we can work around this issue by overriding the value of useModuleDirectories with false, which is possible by specifying the following script tag in the -bottom option:

<script>
if (typeof useModuleDirectories !== 'undefined') {
  useModuleDirectories = false;
}
</script>

Note that you must specify the --allow-script-in-comments option as well so that Javadoc does not complain about using the <script/> tags in comments.


Using the Javadoc maven plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>${javadoc.pluginVersion}</version>
    <executions>
        <execution>
            <id>javadoc</id>
            <goals>
                <goal>javadoc-no-fork</goal>
                <goal>jar</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
                <bottom>
                <![CDATA[
                    <script>
                    if (typeof useModuleDirectories !== 'undefined') {
                      useModuleDirectories = false;
                    }
                    </script>
                ]]>
                </bottom>
                <additionalJOption>--allow-script-in-comments</additionalJOption>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
trustin
  • 12,231
  • 6
  • 42
  • 52
5

This bug should be fixed in JDK 12: https://bugs.openjdk.java.net/browse/JDK-8215291

Credit goes to Chris Povirk for locating the relevant bug report.

Gili
  • 86,244
  • 97
  • 390
  • 689
1

This indeed resolves the «Search» linking to ../undefined/... problem. There is however a nasty side effect: Links to standard classes or interfaces directing towards e.g. https://docs.oracle.com/en/java/javase/12/docs/api/... will be broken due to missing module names.

Consider e.g. links to java.lang.String now incorrectly pointing to https://docs.oracle.com/en/java/javase/12/docs/api/java/lang/String.html rather than towards https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/String.html.

  • I think that "nasty side effect" is a bug/another topic, which could be workaround with `-link` option. See here for more info: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8211194 – Radoslav Ivanov Jan 20 '21 at 23:55
0

adding <additionalJOption>-no-module-directories</additionalJOption>

worked for me. with one - not --