Serene
Serene

Reputation: 13

Java cannot find a class in a .jar file in Linux

I built a java program in Eclipse in Windows and it worked well. The program included 5 java classes and 5 jar library files.

Then I copied all *.java files and *.jar files to Linux. After I compiled and run it, I got an exception, the class PaserException of htmlparser.jar was not found.

Exception in thread "main" java.lang.NoClassDefFoundError: org/htmlparser/util/ParserException
Caused by: java.lang.ClassNotFoundException: org.htmlparser.util.ParserException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)

I just begin to learn Linux so I'm not sure if I compiled and run it correctly.

All of my files(*.java and *.jar) are in the same directory, and my operation is in this directory.

To compile:

javac -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar *.java

To run:(the main method is in Crawler class, main method requires at lease 1 argument)

java -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar Crawler arg0 arg1

Then I got the exception above. Did I compile and run it correctly? Why did I got this exception? Thanks.

Upvotes: 1

Views: 1361

Answers (1)

Simon Fischer
Simon Fischer

Reputation: 1196

The folder that holds the class file (.) is not on the classpath. Add another :. to the classpath:

java -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar:. Crawler arg0 arg1

Upvotes: 2

Related Questions