Search code examples
javajavacjakarta-mail

Compiling java with classpath doesn't find all jar files


I have a lib folder with many .jar files and one of the files I have is mail.jar (for javax.mail)

When I compile my source code, I use

javac -cp .:../lib/ server/MyFile.java

And then I get a bunch of errors

server/MyFile.java:22: error: package javax.mail.internet does not exist
import javax.mail.internet.InternetAddress;
                          ^
server/MyFile.java:23: error: package javax.mail.internet does not exist
import javax.mail.internet.MimeMessage;
                          ^
server/MyFile.java:24: error: package javax.mail does not exist
import javax.mail.Authenticator;
                 ^
etc
etc

However javax.mail should be inside (via mail.jar). I also have activation.jar inside of the lib folder. Note that this mail.jar file I just downloaded today from sun website. It is recent.

What could be wrong?

UPDATE:

I have added all the jar files manually on the classpath, and it compiles fine. However now when I run MyFile.java, using this command

java server.MyFile

I get this error:

 Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
    at java.lang.Class.getMethod0(Class.java:2685)
    at java.lang.Class.getMethod(Class.java:1620)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 6 more

Solution

  • is there an easier way to do this?

    Since version 6, you can use class path wildcards.

        javac -cp .:../lib/* server/MyFile.java
    

    See also this related Q&A.