【问题标题】:External jar files are not detected when compiled using cmd.?使用cmd编译时没有检测到外部jar文件。?
【发布时间】:2019-01-14 11:35:09
【问题描述】:

我已经在 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 文件都正确包含在类路径中。有什么解决办法吗?

【问题讨论】:

标签: java eclipse command-line


【解决方案1】:

使用类路径(-cp)

javac -cp %YOUR_JAR_LOCATION% mail.java

例子:

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

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

【讨论】:

  • 谢谢。您的解决方案有效。但是执行.class文件时出现NoClassDefFoundError异常
  • @LogeshKumar 在执行 .class 文件时也使用类路径 (-cp)
【解决方案2】:

Linux:

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

窗口:

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

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多