使用 cmd 编译时,不会检测到外部 jar 文件



我已经在日食中运行了这个程序,它可以工作.但是当使用命令提示符编译时,使用 javac 邮件.java 或 java 类路径 .myClass ,它会产生一个错误。

这是代码.

public class mail {
private static String USER_NAME = "***";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "****"; // GMail password
private static String RECIPIENT = "****";
private static String RECIPIENT_A="****";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT,RECIPIENT_A }; // list of recipient email addresses
    String subject = "MailCheck";
    String body = "JAVAMAIL check";

    sendFromGMail(from, pass, to, subject, body);
}
private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    System.out.println("props setting over");
    Session session = Session.getInstance(props);
    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];
        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }
        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);   
        }
        System.out.println("array settings over");
        message.setSubject(subject);
        message.setText(body);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        System.out.println("Mail Sending Over");
        transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
}

}

Javax.mail 库存在于同一项目的 lib 文件夹下。使用命令提示符编译时,不会检测到这些.jar文件。

C:WorkspaceMailCheckmailChecksrccomatos>javac mail.java
mail.java:6: package javax.mail does not exist
import javax.mail.*;
^
mail.java:7: package javax.mail.internet does not exist
import javax.mail.internet.*;
^
mail.java:39: cannot find symbol
symbol  : class Session
location: class com.atos.mail
        Session session = Session.getInstance(props);
        ^
mail.java:39: cannot find symbol
symbol  : variable Session
location: class com.atos.mail
        Session session = Session.getInstance(props);
                          ^
mail.java:40: cannot find symbol
symbol  : class MimeMessage
location: class com.atos.mail
        MimeMessage message = new MimeMessage(session);
        ^
mail.java:40: cannot find symbol
symbol  : class MimeMessage
location: class com.atos.mail
        MimeMessage message = new MimeMessage(session);
                                  ^
mail.java:43: cannot find symbol
symbol  : class InternetAddress
location: class com.atos.mail
            message.setFrom(new InternetAddress(from));

此外,所有外部 jar 文件都正确包含在类路径中。任何解决方案 ?

使用 classpath (-cp)

javac -cp %YOUR_JAR_LOCATION% mail.java

例:


javac -cp ".:./jars/mail.jar" helloworld.java java -cp ".:./jars/mail.jar" helloworld

对于 Windows,":"应替换为 ";",并确保 jar 文件路径正确。

linux:

javac -cp lib/mail.jar:lib/activator.jar mail.java

窗户:

javac -cp libmail.jar;lib/activator.jar mail.java

最新更新