How to write the batch files for java classes

Introduction:
Batch file a set of commands executed by command line interpreter to fulfill the certain task.
batch file extension is .bat .These are supported in window systems only because .bat files support in windows systems.
Bat file icon
Here my java class is sending an email to the respective user.
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 javasnipets.info
   @Description:This class send a email to user.
 */
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 = "abc@mail.com";
        String password = "******";
        String to = "xyz@mail.com";
        String sub = "Greetngs From javasnippets";
        //InetAddress localhost = InetAddress.getLocalHost();
        //String msg = localhost.getHostAddress().trim();
        //get Session 
        String msg="Hello user this is the sample email from javasnippets team";  
        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);
        }
    }
}
Here is my bat script
push D:Email
set PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_92\bin
SET CLASSPATH=D:\Email\activation.jar;D:\Email\java-mail-1.4.4.jar;.
javac mail.java
java mail
pause 
Explanation:
  • First tells the command prompt to where is located the java file.
  • Here push command used to open the command prompt project location directory.
  • Here I am using java class right to execute or compiles the java program we need java so here I am mapping the java path use the set path variable.
  • To Execute the java class need supported jar for sending a mail .so here I am adding the jar file using the set classpath variable.
  • Finally, compile the java class using the javac classsname.java.
  • After compilation the program .clss is generated right then we executed the .class file which is generated by JVM
  • To execute the class file using the command java class name
  • Here pause is used for command prompt will not exit after executes the code. Some time error will come while executing the code. in this scenarios command prompt will shows the what is exact error and which line.