All,
The following code works fine and sends email when ran from within eclipse. However when i export from eclipse as a jar file and run the following command from a batch file it gives me error. Note: there are other files in the jar file that work fine when ran from the batch file.
java -cp C:\Users\Administrator\Email.jar LotusNotes.SendEmail
Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingE
xception
at LotusNotes.RunBackUpSourceCopy.main(RunBackUpSourceCopy.java:49)
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
SendEmail class:
public class SendEmail {
public String to ;
public String from;
public String host;
public SendEmail(){
to = "xyz#gmail.com";
from = "localhost";
host = "localhost";}
public void send_production_email(String reportDate){
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Subject Line!");
message.setText("Test email!");
Transport.send(message);
System.out.println("Sent message successfully....");
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Preparing to Send Email ...");
SendEmail email = new SendEmail();
email.send_production_email("test production");
}
}
Looks you need to add javax mail jar to your class path.
Edit
java -cp "C:\Users\Administrator\Email.jar";"C:\Users\Administrator\ExternalJarFiles\mail.jar"; LotusNotes.SendEmail
Based on the comment bellow...
Related
I have created a maven project and writing tests in it using testng. I have also created an executable jar using maven but getting error when executing the jar in command prompt with the arguments that are used in project. I could not actually figure out the error. Please help me with this.
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.NoClassDefFoundError: org/testng/TestNG
at mypackage.App.main(App.java:40)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
command prompt error image in this link
My project Main class codes(App.java)
public class App {
public static String testdataExcel;
public static Properties props = null;
public static Properties loadPropertyFile(String filepath) throws IOException {
FileInputStream stream = new FileInputStream(filepath);
props = new Properties();
props.load(stream);
return props;
}
public static void main(String[] args) throws IOException, InterruptedException {
//Receiving arguments from command prompt
String Filename = args[0];
testdataExcel = args[1];
loadPropertyFile(Filename);
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { LoginPage.class, testclass2.class,
testclass3.class, testclass4.class,
testclass5.class, testclass6.class });
testng.run();
}
}
I have tried to reinstall java, testng as well and then i ran the project but i am getting same error. i could not figure out actual error so far. so kindly help me to find the error and complete this task.
Thanks in advance :)
I am trying to call Twilio SMS API to trigger SMS using my java code. I am using twilio provided supporting library. All looks fine when I run the code in IDE however I am getting below error when convert into Jar and run it by passing the parameter
Exception in thread "main" java.lang.NoClassDefFoundError:
com/twilio/Twilio
at twilioSMS.TwilioSmsMsging.main(TwilioSmsMsging.java:15) Caused by: java.lang.ClassNotFoundException: com.twilio.Twilio
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Below is my code
package twilioSMS;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
public class TwilioSmsMsging {
public static void main(String[] args) {
String ACCOUNT_SID = args[0];
String AUTH_TOKEN = args[1];
String toMobileNumber = args[2];
String fromMobileNumber = args[3];
String smsMessage = args[4];
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator(new com.twilio.type.PhoneNumber(toMobileNumber),new com.twilio.type.PhoneNumber(fromMobileNumber),smsMessage).create();
System.out.println(message.getSid());
}
}
My bad.. I have to export as runnable jar however I have exported as jar. Now issue resolved after exporting as runnable jar.
I am trying to send push notification using notnoop java library but i am receiving SSL exception. I have also tried to search the solution online and took help of some online tools, but in vain.
I tried "http://pushtry.com/" which is showing SSPI error and "http://www.pushwatch.com/apns/" showing "[Errno 1] _ssl.c:510: error:14094438:SSL routines:SSL3_READ_BYTES:tlsv1 alert internal error".
I do not have idea on ios but i want the solution for a project.
Scenario: External java server should send push notification to particular ios app.
My Code:
import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.EnhancedApnsNotification;
public class ApnsTest {
private ApnsService service;
public ApnsTest() {
try {
String certPath = "certification_ios.p12";
File certFile = new File(certPath);
InputStream fis = new FileInputStream(certFile);
service = APNS.newService().withCert(fis,"password").withSandboxDestination().build();
sendMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ApnsTest();
}
public void sendMessage() {
String payload=APNS.newPayload().alertBody("").sound("default").build();
String deviceToken ="devicetokenof64characters";
int now = (int) (new Date().getTime() / 1000);
EnhancedApnsNotification notification = new EnhancedApnsNotification(EnhancedApnsNotification.INCREMENT_ID(),
now + 60 * 60, deviceToken, payload);
System.out.println("Sending push notification");
service.push(deviceToken, payload);
}
}
The full stacktrace of the exception i am getting is below:
com.notnoop.exceptions.NetworkIOException: javax.net.ssl.SSLException: Received fatal alert: internal_error
at com.notnoop.apns.internal.Utilities.wrapAndThrowAsRuntimeException(Utilities.java:277)
at com.notnoop.apns.internal.ApnsConnectionImpl.sendMessage(ApnsConnectionImpl.java:257)
at com.notnoop.apns.internal.ApnsConnectionImpl.sendMessage(ApnsConnectionImpl.java:230)
at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:46)
at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:56)
at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:36)
at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:45)
at ApnsTest.sendMessage(ApnsTest.java:47)
at ApnsTest.<init>(ApnsTest.java:24)
at ApnsTest.main(ApnsTest.java:31)
Caused by: javax.net.ssl.SSLException: Received fatal alert: internal_error
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at java.io.OutputStream.write(Unknown Source)
at com.notnoop.apns.internal.ApnsConnectionImpl.sendMessage(ApnsConnectionImpl.java:240)
... 8 more
What am i missing or doing wrong?
Password is correct and device token provided is also a valid one.
Does anyone have a example about how to make a java mongoDB connector to use with JasperReports? I have been trying make it work but without any sucess... All example on internet looks like old versions of JasperReports...
I tried this code
GeneratePDF with JasperReports Library and MongoDB
String mongoURI = "mongodb://bdsandbox6:27017/test";
MongoDbConnection connection = null;
Map<String, Object> parameters = new HashMap<String, Object>();
try {
connection = new MongoDbConnection(mongoURI, null, null);
parameters.put(MongoDbDataSource.CONNECTION, connection);
File jasperFile;
jasperFile = new File("MongoDbReport.jasper");
JasperCompileManager.compileReportToFile("MongoDbReport.jrxml", "MongoDbReport.jasper");
JasperFillManager.fillReportToFile("MongoDbReport.jasper", parameters);
JasperExportManager.exportReportToPdfFile("MongoDbReport.jrprint");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
But I ways got this error:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/log4j/Logger at
com.jaspersoft.mongodb.connection.MongoDbConnection.(MongoDbConnection.java:64)
at serverTeste.Rest.main(Rest.java:22) Caused by:
java.lang.ClassNotFoundException: org.apache.log4j.Logger at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more
I know it seens I need log4j.jar but i tried it and I faill again. I guess I am using the wrong jars files...
I also tried it:
GeneratePDF with JasperReport Library and MongoDB
Without Sucess
All I want to do is send an email in Java. Here is the example I found:
import org.apache.commons.mail.SimpleEmail;
public class Email {
public static void sendMessage(String emailaddress, String subject, String body) {
try {
SimpleEmail email = new SimpleEmail();
email.setHostName("valid ip address here");
email.addTo(emailaddress);
email.setFrom("noreply#example.com", "No reply");
email.setSubject(subject);
email.setMsg(body);
email.send();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I immediately get the following exception on the SimpleEmail email = new SimpleEmail(); line:
java.lang.ClassNotFoundException: org.apache.commons.mail.SimpleEmail
I have the following JAR in my project (using Netbeans):
commons-email-1.2.jar
What am I doing wrong?
Thanks.
I tried to run your code and got this:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Message
at javaapplication3.Email.sendMessage(Email.java:9)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:7)
Caused by: java.lang.ClassNotFoundException: javax.mail.Message
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 2 more
Java Result: 1`
Appears that some import is missing. You need the javax.mail package to run your code.
Not sure why SimpleEmail didn't work. But this did:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class Email {
public static void sendMessage(String emailaddress, String subject, String body) {
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "myhost");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail plain");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("test#example.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Thanks for the suggestions.
The SimpleEmail class is in there, and you have the correct package, so I'd have to say this is a problem in NetBeans configuration for your project.
Since this is a simple example, you could try compiling from the command-line and adding the JAR to your classpath that way. It should work. Then you can figure out why it's not being picked up in NetBeans.
You could also add the source code of the project to your src folder. But most of the times a clean & build helps with these problems.
If nothing helps, close netbeans, delete manually the build & dist folder of your project, start netbeans up again and perform a clean & build. Helped me once out of a mysterious problem.