My friends and I have minecraft server and we want to add JavaMail plugin with Maven , We added 2 jar files:
Mail.jar
Activation.jar
With this code:
package com.parlagames;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class App {
public void AppVoid(String host, String port,final String userName,final String password, String[] toAddress, String subject, String message) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port",port);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
try {
Message SendMessage = new MimeMessage(session);
SendMessage.setFrom(new InternetAddress(userName));
for(int i=0;i<toAddress.length;i++) {
SendMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress[i]));
SendMessage.setSubject(subject);
SendMessage.setContent(message, "text/html; charset=utf-8");
Transport.send(SendMessage);
}
System.out.println("Sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
When we start the plugin in his server it shows an error that it doesn't identify the class
java.lang.NoClassDefFoundError: com/parlagames/App
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_161]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_161]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_161]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_161]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_161]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
Why does it happend? we need to have the maven by the way
It seems that you need to add this line of code to the plugin:
public void onEnable()
and this code
public void onDisable()
It also seems that you don't have a main class. A main class is declared at plugin.yml. Try finding the part that says "main:" and change it to the class that has the "onEnable()" and "onDisable()". Also add extends JavaPlugin as someone said before
Is App the main class of your plugin ? If so, it you need to make it extend the JavaPlugin class like this :
public class MyPlugin extends JavaPlugin {
public void onEnable() {
}
public void onDisable() {
}
}
If you have trouble understanding of the bukkit/spigot API, I would suggest to start learning from the docs (here is a reference guide for the basics).
Related
I am trying to send an email in my JavaFX application
Everything works perfect, email is sent to Reciepent, i get no exceptions and no errors when i run it in IDE (InteliJ) but when I run the app outside Intelij it doesn't work.
I made simple registration form which saves data from fields into my database:
String username;
String password;
String email;
Random rd = new Random();
int ID;
public void registerUser(javafx.event.ActionEvent ab) {
username = textUser.getText();
password = textPass.getText();
email = textEmail.getText();
ID = rd.nextInt(999999999);
Connection connectt = null;
try {
Class.forName("org.sqlite.JDBC");
connectt = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\barte\\OneDrive\\Desktop\\sqlite databases\\PRODUCTS\\Products.db");
String s = "INSERT INTO Users(Username,Password,Email,UserID) VALUES (?,?,?,?) ";
PreparedStatement registera = connectt.prepareStatement(s);
registera.setString(1, username);
registera.setString(2, password);
registera.setString(3, email);
registera.setInt(4, ID);
System.out.println(username);
System.out.println(password);
System.out.println(email);
registera.executeUpdate();
System.out.println("Added to Database");
sendMail();
registerr.setStyle("-fx-background-color: #69ff59;");
registerr.setText("Check Your MailBox");
registerr.setOnMouseClicked(event -> {
registerr.setText("Email Has been sent");
});
textUser.setText(null);
textEmail.setText(null);
textPass.setText(null);
regiPane.setVisible(false);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
And here is code for sending email:
public void sendMail() throws MessagingException {
String USER_NAME = "stoc****";
String from = USER_NAME;
String PASSWORD = "************";
String pass = PASSWORD;
String RECIPT = textEmail.getText();
String TOPIC = "Welcome " + username + "!";
String BODY = "Dear user! " +
"You can sign into StockFX by your ID/Username and password" +
"User ID: " + ID + "\n" + "Password: " + password + "\n" +
"We would like to thank you for using our services now and in future!";
String[] to = {RECIPT};
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");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"****", PASSWORD);
}
});
MimeMessage message = new MimeMessage(session);
try {
try {
message.setFrom(new InternetAddress(from));
} catch (MessagingException e) {
e.printStackTrace();
}
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
try {
toAddress[i] = new InternetAddress(to[i]);
} catch (AddressException e) {
e.printStackTrace();
}
}
for (int i = 0; i < toAddress.length; i++) {
try {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
} catch (MessagingException e) {
e.printStackTrace();
}
}
try {
message.setSubject(TOPIC);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.setText(BODY);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.saveChanges();
} catch (MessagingException e) {
e.printStackTrace();
}
registerr.setStyle("-fx-background-color: #69ff59;");
registerr.setText("You can now log in");
registerr.setDisable(false);
textUser.setText(null);
textEmail.setText(null);
textPass.setText(null);
regiPane.setVisible(false);
Transport transport = session.getTransport("smtp");
System.out.println("get protocl");
transport.connect(host, from, pass);
System.out.println("get host,from and password");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("get recipients");
transport.close();
System.out.println("close");
System.out.println("Email Sent Successfully!");
} finally {
System.out.println("Complete Process");
}
}
Everything works fine inside InteliJ but void sendEmail won't work in runable jar
I am new to Java mail.
Imports:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.*;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;
This class is Controller Class
And this is the main Class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage UI) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("DashBoard.fxml"));
UI.setTitle("DIREXT SCANNER (DEMO VER 0.5)");
UI.setScene(new Scene(root, 800, 600));
UI.initStyle(StageStyle.UNDECORATED);
UI.setResizable(false);
UI.show();
UI.setFullScreenExitHint("Press 'ESC' to exit full screen");
}
public static void main(String[] args){
launch(args);
}
}
I tried rebuilding project, delete and add libraries again but the same result
Did anybody elese encountered the same problem?
Is it IDE related or am I missing imports or methods?
I tried to look for similar question on forums, I have already fixed few things, as before runable jar wouldn't run at all.
If question exist please can someone provide the link.
edit
this is the error i get when i run jar from PowerShell:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Node.fireEvent(Unknown Source)
at javafx.controls/javafx.scene.control.Button.fire(Unknown Source)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
... 52 more
Caused by: java.lang.NoClassDefFoundError: javax/activation/DataHandler
at sample.DashBoardController.registerUser(DashBoardController.java:321)
... 62 more
Caused by: java.lang.ClassNotFoundException: javax.activation.DataHandler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 63 more
I found solution thanks to all your help
What i did, in project structure, I deleted all artifact, modules and libraries and add everything back again then rebuild the project then build the artifact and I also did set my project folder as Source Root and it worked.
If you are using External Libraries So The problem Sometimes Happens in the Path of this Libraries When you extract to jar File , Simple Solution that Sometimes Work is to create new project and create new Files As the Old Project And move them
I'm trying to start using Quartz. I'm trying to do a simple sample app, but i'm getting this error: java.lang.IncompatibleClassChangeError
I hope someone can help me solve this please!
So, this is my code:
InvokingTask.java:
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class InvokingTask implements Job {
public void execute(JobExecutionContext jec) throws JobExecutionException {
System.out.println("test --- "+new Date());
//Aca pueden poner la tarea o el job que desean automatizar
//Por ejemplo enviar correo, revisar ciertos datos, etc
}
}
Scheduling.java:
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class Scheduling {
private Scheduler horario;
private void crearProgramacio() {
try {
SchedulerFactory factoria = new StdSchedulerFactory();
horario = factoria.getScheduler();
horario.start();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void iniciarTarea() {
if (this.horario == null) {
this.crearProgramacio();
}
try {
JobDetail job1 = JobBuilder.newJob(InvokingTask.class).withIdentity("job1", "group1").build();
Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("cronTrigger1", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
Scheduler scheduler1 = new StdSchedulerFactory().getScheduler();
scheduler1.start();
scheduler1.scheduleJob(job1, trigger1);
} catch (SchedulerException ex) {
System.out.println(ex.getMessage());
}
}
}
Test.java:
public class Test {
public static void main(String[] args) {
Scheduling test = new Scheduling();
test.iniciarTarea();
}
}
This is the error I'm getting:
Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at com.motorbox.logic.Scheduling.iniciarTarea(Scheduling.java:41)
at com.motorbox.logic.Test.main(Test.java:16)
Please, any suggestions?
I just realiced that I was using an old librery. I updated it and now it's working!!
I'm trying to create an administrative client program for websphere,
but when I'm trying to connect I get the following message.
Maybe I lack some libs (I create my app in notepad).
at TryConnection1.main(TryConnection1.java:37) Caused by: java.lang.ClassNotFoundException: com.ibm.websphere.security.auth.WSL oginFailedException
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)
... 1 more
My code:
import java.util.Properties;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.*;
import com.ibm.websphere.management.*;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.*;
import com.ibm.websphere.management.exception.ConnectorException;
public class TryConnection1 {
/** * #param args */
public static void main(String[] args) {
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, "hostgoeshere");
connectProps.setProperty(AdminClient.CONNECTOR_PORT, "portgoeshere");
connectProps.setProperty(AdminClient.USERNAME, "usernamegoeshere");
connectProps.setProperty(AdminClient.PASSWORD, "passgoeshere");
AdminClient adminClient = null;
try {
adminClient = AdminClientFactory.createAdminClient(connectProps);
} catch(ConnectorException e) {
System.out.println("Exception creating admin client: " + e); }
}
}
try to add $WEBSPHERE_HOME/AppServer/runtimes/com.ibm.ws.admin.client_8.5.0.jar, or similar if you're using a different WebSphere version, to your classpath. This is the required jar for WebSphere Admin Client.
You should try to add:
import com.ibm.websphere.security.auth.*;
You people are making me self conscious.. I will try to make this one better.
Okay, so this program (Don't kill me.. I downloaded it) is only working on Windows 7 and Ubuntu as far as I can tell. When you open it on Windows 8 it says "Java exception Error."
I'm thinking this has something to do with catch(messagingException ex) at the end of the file. I admit, I don't know a whole lot about java, but you have to start somewhere.. don't you? I do know java is for all platforms!
I have also tried this program with multiple files and multiple Gmail accounts... I even tried it with my Comcast email address.
I'm using the "JavaMailAPI" (http://www.oracle.com/technetwork/java/javamail/index.html) for the actual mailing part.
When I open it on terminal in Windows 8 it gives me this:
Exception in thread "Main" java.lang.noclassdeffounderror: java/mail/mailexception
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.langf.Class.privateGetDdecLaredMethods(Unkown source)
at java.lang.Class.getMethod(unknown source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown source)
caused by: java.lang.classnotfoundexception: java.mail.messagingException
at java.net.URLCLassLoader$1.run(unknown source)
at java.net.URLClassLoader$1.run(Unknown source)
at java.security.AccessController.doPrivaleged(native Method)
at java.net.URLClassLoader.findClass(Unknown source)
at java.lang.ClassLoader.findClass(Unkown source)
at jaa.lang.CLassLoader.loadClass(Unknown source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown source)
at java.lang.ClassLoader.loadClass(Unknown source)
... 6 more
Here is the code:
package testing;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Testing{
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("Email","Password");
}
});
try {
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("Email Address"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("Email Address to send "));
message.setSubject("Subject");
message.setText("Message");
String filename = "attachment location";
DataSource source = new FileDataSource(filename);
message.setDataHandler(new DataHandler(source));
message.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException ex) {
Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
If you need anything else about the program please ASK!
You have to download Java Mail
If JavaMail is needed by this application you should build an installer to package and install the needed JARs as JavaMail.
Source code:
package com.web;
import com.web.Operation;
import java.applet.*;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.io.*;
public class AppletExample extends Applet {
public void init() {
try {
getAppletContext().showDocument(new URL("file:///C:/Users/Victor/Desktop/test.txt"), "_blank");
}
catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
}
}
public void paint( Graphics g ) {
Operation op = new Operation();
op.response();
g.drawString("Go File", 0,100);
}
}
When I run the Applet using the Appletviewer application the next error comes on screen:
C:\Users\Victor\Desktop\project2\src>appletviewer display.html
Warning: Can't read AppletViewer properties file: C:\Users\Victor\.hotjava\prope
rties Using defaults.
java.security.AccessControlException: access denied ("java.io.FilePermission" "C
:\Users\Victor\Desktop\test.txt" "write")
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:366)
at java.security.AccessController.checkPermission(AccessController.java:
555)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
at java.io.FileOutputStream.<init>(FileOutputStream.java:203)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at java.io.FileWriter.<init>(FileWriter.java:63)
at com.web.Operation.response(Operation.java:15)
at com.web.AppletExample.paint(AppletExample.java:25)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
at sun.awt.RepaintArea.paint(RepaintArea.java:240)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:347)
at java.awt.Component.dispatchEventImpl(Component.java:4936)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:117)
This is what the class Operation does:
package com.web;
import com.web.AutomatedTelnetClient;
import java.util.*;
import java.io.*;
public class Operation {
public Operation() {
}
public void response() {
try {
BufferedWriter out = new BufferedWriter(new FileWriter("C://Users/Victor/Desktop/test.txt"));
AutomatedTelnetClient telnetClient = new AutomatedTelnetClient();
telnetClient.connect();
StringBuffer text = telnetClient.sendCommand("display gps");
telnetClient.disconnect();
out.write(text.toString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
It seems to be a permission problem to write in the file from the applet, isn't it? How can I solve it?
Since it is a file system of the client you are trying to write to. You need to create a jar with the class files and have to sign it as a trusted application. Follow this post on how to sign, then you that jar to load the applet.