Search code examples
javajarjakarta-mailjavac

How to compile a java file which uses another java class that requires jar files?


I have written a java file(called SendEmail.java) which uses javax.mail.jar and javax.activator.jar for compilation and running.
Now I have written another java file(called Check.java) which imports SendEmail.
The problem I am facing here is while I am compiling Check.java. I compile it using the same classpath as I used while compiling SendEmail.java. The files are in a package called pack.

Command I use to compile SendEmail.java -

C:\Users\Myname\code\javafiles>javac -cp ".;.\pack\javax.activation.jar;.\pack\ja
vax.mail.jar;" pack\SendEmail.java

Command I use to compile Check.java -

C:\Users\Myname\code\javafiles>javac -cp ".;.\pack\javax.activation.jar;.\pack\ja
vax.mail.jar;" pack\Check.java 

The error I am getting is-

C:\Users\Myname\code\javafiles>javac -cp ".;.\pack\javax.activation.jar;.\pack
\javax.mail.jar;" pack\Check.java  
pack\Check.java:3: error: cannot access SendEmail  
import pack.SendEmail.*;  
           ^
  bad class file: .\pack\SendEmail.class  
    class file contains wrong class: SendEmail  
    Please remove or make sure it appears in the correct subdirectory of the cla
sspath.

Solution

  • I think you want:

    javac -cp ".;.\pack\javax.activation.jar;.\pack\javax.mail.jar;" pack\Check.java
    

    And yes, you should really move the jar files out of the directory containing your source and class files.