Erik Vesterlund
Erik Vesterlund

Reputation: 451

Generate javadoc for package with jar dependencies

how do you generate javadoc for a package, if a source file in that package depends on some external library (jar)?

Here's an environment and javadoc generation for a package without library dependencies:

mkdir -p ~/doctest/com/foo/bar/baz
cd ~/doctest
cat > com/foo/bar/baz/Glorp.java << EOF
package com.foo.bar.baz;
public class Glorp {
  /**
   * Here's some javadoc.
   *
   * @return A magic number
   */
   public static void foo() { return 42; }
}
EOF
javadoc -d doc com.foo.bar.baz

Now suppose it depends on, say, commons-logging-1.2.jar, how do you generate javadoc? You can't just run the above command, javadoc will complain that it has no idea what Logger is.

wget http://ftp.download-by.net/apache//commons/logging/binaries/commons-logging-1.2-bin.tar.gz
mkdir jars
tar xf commons-logging-1.2-bin.tar.gz --one-top-level=jars
sed -i 's/package.*/&\nimport org.apache.log4j.Logger;/' com/foo/bar/baz/Glorp.java
javadoc -d doc com.foo.bar.baz
Loading source files for package com.foo.bar.baz...
Constructing Javadoc information...
./com/foo/bar/baz/Glorp.java:2: error: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
                       ^
1 error

Just adding jars on the classpath doesn't seem to work either:

javadoc -classpath jars/ -d doc com.foo.bar.baz
Loading source files for package com.foo.bar.baz...
javadoc: error - No source files for package com.foo.bar.baz
1 error

At this point I'm out of ideas. Any pointers on how to do it? Thank you.

(When I google for this problem I only find answers involving Maven, Gradle or other such programs, but I don't want to use them, this question is about javadoc, not build tools or IDEs.)

Upvotes: 2

Views: 799

Answers (1)

Robert
Robert

Reputation: 8561

Can you compile your code? The Javadoc classpath settings should be just the same as for compiling.

Looking at the jar file you downloaded, it doesn't seem to have the log4j classes:

unzip -l commons-logging-1.2.jar | grep log4j

comes up empty.

When I replace

import org.apache.log4j.Logger;

with one of the classes in the jar you downloaded, e.g.,

import org.apache.commons.logging.impl.Log4JLogger;

and set the classpath to both the jars path and the current directory (so that it finds your source code), it works:

javac -cp .:jars/commons-logging-1.2/commons-logging-1.2.jar com/foo/bar/baz/Glorp.java 
javadoc -cp .:jars/commons-logging-1.2/commons-logging-1.2.jar  -d doc com.foo.bar.baz

Upvotes: 2

Related Questions