How to write the batch for sample java class and scheduling task in task scheduler

Introduction:
Basically, windows allow .bat file Linux allows .sh file here I am writing the batch file for sample java program.

Here my java class fetch my system IP number and send an email to the specified user which I am provided in java class.
ScriptFile

Here is my java class
import java.net.InetAddress;
import java.net.UnknownHostException;
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;

/**
 *
 * @author Ram Janardhan Randhi
 */
public class mail {

    public static void main(String[] args) throws UnknownHostException {
        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", "587");
        String from = "****************"; //email id 
        String password = "***************"; //password
        String to = "**********"; //Destination(Receiver) email
        String sub = "Updated IP_Address";
        InetAddress localhost = InetAddress.getLocalHost();
        String msg = localhost.getHostAddress().trim();
        //get Session   
        Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() 
        {
            protected PasswordAuthentication getPasswordAuthentication() 
            {
                return new PasswordAuthentication(from, password);
            }
        });
        //compose message    
        try {
            MimeMessage message = new MimeMessage(session);
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(sub);
            message.setText(msg);
            //send message  
            Transport.send(message);
            System.out.println("message sent successfully");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
Explanation:
  • Here I am providing the Gmail SMTP details for sending emails to users.
  • First, create the properties class instance. it used to load the properties of SMTP.
  • Using the javax.mail.Authenticator() method used for weather user given details authenticated or not.
  • Using the session object(authentication response)  to be the mime message instance.
  • Using predefined methods like addRecipient,setSubject,setText, send methods to send an email to the destination user.
  • Here i am using the InetAddress localhost = InetAddress.getLocalHost();   String msg = localhost.getHostAddress().trim();  these are used for getting the ip address of my system.
Here is my bat file
push D:\projects\DailySchedular
set PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_92\bin
SET CLASSPATH=D:\projects\DailySchedular\activation.jar;D:\projects\DailySchedular\java-mail-1.4.4.jar;.
javac mail.java
java mail

Explanation:
  • push command is used for command line editor change the directory to your project location.
  • set path used for setting the java path. (java executable file location).
  • set classpath used to add the libraries which are used to compile the java program.
  • javac  filename.java used to compile the java program.
  • java filename used for executing the class file.
TO SET THE SCHEDULER TASK IN TASK IN TASK SCHEDULER TO SEE THE BELOW VIDEO

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

0 Response to "How to write the batch for sample java class and scheduling task in task scheduler"

Post a Comment