使用cmd编译时,未检测到外部jar文件。

时间:2019-01-14 11:35:10

标签: java eclipse command-line

我已经在eclipse中运行了该程序,并且可以运行。但是在使用命令提示符使用javac mail.java或java -classpath进行编译时。 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:\Workspace\MailCheck\mailCheck\src\com\atos>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中。有解决方案吗?

2 个答案:

答案 0 :(得分:0)

使用类路径(-cp)

javac -cp %YOUR_JAR_LOCATION% mail.java

示例:

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

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

答案 1 :(得分:0)

linux:

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

windows:

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