tags:

views:

64

answers:

4

I wrote the basic code below and saved to a file called pdf.java.

package pdf;

import java.util.*;
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.util.regex.*;

public class pdf {

    public static void main(String[] args) throws IOException, DocumentException{
        System.out.println("Hello World2!");
    }

}

I then compiled it like this,

javac pdf.java -cp core-renderer.jar:iText-2.0.8.jar

Which seemed to work as I got a pdf.class file. I then tried to run it with the following command.

java pdf

And I got the following output,

Exception in thread "main" java.lang.NoClassDefFoundError: pdf (wrong name: pdf/pdf)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)

What am I doing wrong?

Thanks in advance.

+5  A: 

Didn't you mean java pdf.pdf as your pdf class is in the pdf package ?

If your pdf class is in the pdf package it should be in a pdf directory (as in if your MyClass class is in my.package package, it should be in my/package/ directory).

Either you go into the pdf directory and use the -d option javac -d . pdf or you go in the parent directory and do javac pdf/pdf.java`

Colin Hebert
I don't see a pdf package declaration but you are almost certainly right :)
Hemal Pandya
Sorry, I reverted it again, it should be as you saw it before now.
usertest
So the problem was the pdf package. You're luky I saw that, because it would have been harder to debug without the package declaration.
Colin Hebert
No, once he writes `java pdf.pdf` he will get `ClassNotFoundException`.
Hemal Pandya
Colin Hebert
Hi Colin, its not fixed, I'm not sure its the pdf package.
usertest
Did you looked at the updates ?
Colin Hebert
+1  A: 

When you compiled it so, the pdf.class was generated in current directory. Change it to:

javac -cp core-renderer.jar:iText-2.0.8.jar -d . pdf.java 

Which will generate pdf.class in ./pdf subdirectory. Then run it as follows:

java -cp .:core-renderer.jar:iText-2.0.8.jar -d . pdf.java 
Hemal Pandya
Hi, for the second command I get - Unrecognized option: -dCould not create the Java virtual machine.
usertest
It's a bad copy/past, I think the command meant was `java -cp .:core-renderer.jar:iText-2.0.8.jar pdf.pdf`
Colin Hebert
Yes, indeed it was. I corrected it soon after hitting the post button but user201140 got there before me.
Hemal Pandya
Thanks Hemal and Colin, that does it.
usertest
A: 

First, since the class is declared as in a package, you have to store it and call it using the package path.

You need to create a directory structure that matches the package structure. In your case, you need to create a directory called pdf and move pdf.class into it.

Then you would call with `java pdf.pdf' as pointed out in Colin's answer.

This might be sufficient to run your current sample code, since it doesn't really do much of anything. Once you get to adding more functionality, you will likely need to include your libraries on the classpath when executing, as described in Richard's answer.

BTW, it is conventional in Java programming to initcap class names, e.g. Pdf would be the class name in your example, while pdf would be the package name.

Dave Costa
I tried moving pdf.class into a pdf directory and ran java pdf.pdf, but that doesn't seem to work.
usertest
A: 

Classpath for compile-time and for run-time are two different things. However, they are often identical since compilation dependencies are identical as runtime dependencies. Since your main() method throws something defined in iText-2.0.8.jar (I think), you sould define your classpath accordingly:

java -cp .:iText-2.0.8.jar pdf.pdf
gawi