teunw
teunw

Reputation: 11

Java import classes from jar file

I have a Class written which imports some classes from JavaMail. When the application runs, it downloads the file automatically and saves it in a directory called depend. But I can't get it to import the classes from the Javamail jar I downloaded. For Example, I have:

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

At the top of the file, these classes are located in (Path to program/depend/javax.mail.jar)

Upvotes: 1

Views: 8522

Answers (2)

Robin Green
Robin Green

Reputation: 33033

It sounds like you want to load classes over the network.

This can only be done using a ClassLoader. It cannot be done any other way.

It can be done using RMI - but that uses a ClassLoader internally.

Upvotes: 0

cmd
cmd

Reputation: 11841

You must ensure that the javamail.jar file is on the classpath.

You can do this using -classpath arg or by setting the CLASSPATH env var.

  • Here is documentation from Oracle describing java -classpath command line arg
  • Here is documentation describing how to set CLASSPATH environment var.

Upvotes: 3

Related Questions