我已经进行了充分的谷歌搜索并阅读了很多关于这些问题的信息,但仍然无法弄清楚。
我试图SendEmail.java
通过指定类路径从命令行运行我的,以便它可以从任何地方运行。我目前在 /Users/{username} 文件夹中
当我做
javac -cp ":/Users/.../src/*:/Users/.../src/lib/jar/*:/Users/.../src/org/*:/Users/.../src/data/*" /Users/.../SendEmail.java
它编译并创建类文件。
当我运行它时
java -cp ":/Users/.../src/*:/Users/.../src/lib/jar/*:/Users/.../src/org/*:/Users/.../src/data/*" SendEmail
它给出了下面提到的例外。请注意,当我从同一目录运行它时:就像在java SendEmail
同一目录中一样,它运行得非常好。
Exception in thread "main" java.lang.NoClassDefFoundError: /Users/.../SendEmail
Caused by: java.lang.ClassNotFoundException: .Users.XXX.XXX.XXX.XXX.XXX.XXX.SendEmail
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
任何帮助,将不胜感激。
程序:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.lang.Readable;
public class SendEmail
{
public static void main(String [] args)
{
String to = "xxxxxxx"; // Recipient's email ID needs to be mentioned.
String host = "localhost";
String from = "root";
String pwd = "root";
int port = 25;
Properties properties = System.getProperties();
properties.put("mail.transport.protocol","smtp");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pwd);
properties.put("mail.smtp.port", "25"); // 587 is the port number of yahoo mail
Session session = Session.getDefaultInstance(properties, null); // Get the default Session object.
try{
MimeMessage message = new MimeMessage(session);// Create a default MimeMessage object.
message.setFrom(new InternetAddress(from)); // Set From: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set To: header field of the header.
message.setSubject("Cohort revenue");// Set Subject: header field
// MixpanelDate endingDate = new MixpanelDate();
// String filename = "data/cohort_" + endingDate + ".txt";
message.setText(readfile("data/cohort_2013-06-27.txt")); // Now set the actual message
Transport.send(message); // Send message
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
public static String readfile (String filename) {
File file = new File(filename);
String theString = "";//the final string
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
theString = theString + line + "\n";
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return theString;
}
}