データベースをチェックしてメールを送信するデスクトップアプリケーションを作成しました。netbeans 7.2.1 では、アプリケーションは IDE のブートストラップ警告で正常に動作します。しかし、ダブルクリックすると jar ビルドが応答しません。jar ファイルがターミナルのコマンドラインから実行されると、No Class Found Exception javax.mail が表示されます。 Authenticator.Is クラスパスの設定は私の問題ですか? メールの送信に使用されるコード。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Desk_mail
{
private String from = "mailid";
private String subject = "Test Mail";
private String smtpServ = "smtp.gmail.com";
DBConnection dbcon = new DBConnection();
Desk_mailPojo dmp ;
public static void main(String[] args) throws Exception
{
try{
int status;
Desk_mail m1=new Desk_mail();
status=m1.changestatus();
if(status==1)
{
System.out.println("Email sent succesfull");
}
}
catch(Exception e){
try{
Desk_mail m1=new Desk_mail();
String p=e.getMessage()+e.getStackTrace();
m1.sendmail(p, "mailid");
}
catch(Exception en){
throw new Exception("changestatus" + en.getMessage());
}
throw new Exception("main" + e.getMessage());
}
}
public int changestatus() throws Exception
{
String status,email,body;
String policyname,policyno;
int remdays;
int detailsid;
Date remdate ;
Date enddate;
Date expdate;
readxml rl= new readxml();
dbcon.results = new ArrayList();
try{
Desk_mail m1=new Desk_mail();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println(dateFormat.format(date));
dbcon.query="-----MYSQL QUERY----- ";
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.rs = dbcon.pst.executeQuery();
while(dbcon.rs.next()) {
dbcon.results.add(new Desk_mailPojo(dbcon.rs.getInt(1), dbcon.rs.getString(2),dbcon.rs.getString(3),dbcon.rs.getDate(4), dbcon.rs.getString(5), dbcon.rs.getString(6), dbcon.rs.getString(7)));
}
Iterator iter = dbcon.results.iterator();
while (iter.hasNext())
{
Desk_mailPojo dmp= (Desk_mailPojo)iter.next();
enddate=dmp.getDate();
email=dmp.getEmail();
policyname=dmp.getPolicyname();
policyno=dmp.getPolicyno();
detailsid = dmp.getDetailsid();
remdays=Integer.parseInt(dmp.getRemindme());
Calendar c = Calendar.getInstance();
c.setTime(enddate);
c.add(Calendar.DAY_OF_MONTH, -remdays);
String output = dateFormat.format(c.getTime());
remdate= dateFormat.parse(output);
c.setTime(enddate);
c.add(Calendar.DATE, 5);
expdate = dateFormat.parse(dateFormat.format(c.getTime()));
if(date.before(remdate)){
dbcon.query="-----MYSQL QUERY-----" ;
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.pst.setInt(1, detailsid);
dbcon.result=dbcon.pst.executeUpdate();
}
else if (((date.after(remdate))||(date.equals(remdate)))&&(date.before(enddate))){
dbcon.query="-----MYSQL QUERY-----" ;
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.pst.setInt(1, detailsid);
dbcon.result=dbcon.pst.executeUpdate();
if (dbcon.result>0){
body="The insurance policy "+policyname+"with policyno:"+policyno+" will be expired on "+ enddate;
m1.sendmail(body, email);
}
}
else if((date.after(remdate))&&(date.after(enddate))){
dbcon.query="-----MYSQL QUERY----- " ;
dbcon.pst = dbcon.getConnection().prepareStatement(dbcon.query);
dbcon.pst.setInt(1, detailsid);
dbcon.result=dbcon.pst.executeUpdate();
if ((dbcon.result>0)&&(date.before(expdate))){
body="The insurance policy "+policyname+"with policyno:"+policyno+" has been expired on "+ enddate;
m1.sendmail(body, email);
}
}
}
}catch(Exception e){
throw new Exception("changestatus" + e.getMessage());
}
return dbcon.result;
}
public int sendmail(String message,String email) throws Exception
{
try{
Properties props =System.getProperties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth","true");
Authenticator auth = new Desk_mail.SMTPAuthenticator();
Session session=Session.getInstance(props,auth);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
msg.setSubject(subject);
msg.setText(message);
msg.setHeader("MyMail", "Mr. XYZ" );
msg.setSentDate(new Date());
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
msg.setContent(multipart);
Transport.send(msg);
System.out.println("Message sent to"+email+" OK." );
return 0;
}
catch(Exception e)
{
return -1;
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
@Override
public PasswordAuthentication getPasswordAuthentication()
{
String username = "mailid"; // specify your email id here (sender's email id)
String password = "*****"; // specify your password here
return new PasswordAuthentication(username, password);
}
}
}
また、私は xml を読んでいます。そのクラスのコードは次のとおりです。
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class readxml {
public static void main(String argv[]) throws Exception {
try {
String path=System.getProperty("user.dir");
path=path+"/dbconfig.xml";
File fXmlFile = new File(path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("db");
// String username
System.out.println("-----------------------");
String username;
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// url = getTagValue("username", eElement);
// driver = getTagValue("username", eElement);
// username = getTagValue("username", eElement);
// password = getTagValue("username", eElement);
//
System.out.println("URL" + getTagValue("url", eElement));
System.out.println("driver" + getTagValue("driver", eElement));
System.out.println("username" + getTagValue("username", eElement));
System.out.println("password " + getTagValue("password", eElement));
}
}
} catch (Exception e) {
try{
throw new Exception("" + en.getMessage());
}
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
これは、このコマンドを使用してjarファイルを実行しようとしたときのコマンドラインの応答です:java -jar Myjarfile.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
at java.lang.Class.getMethod0(Class.java:2694)
at java.lang.Class.getMethod(Class.java:1622)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more