Running a batch script from Java(Arguments for Bat file) and output of run in a file

 Running batch script using JAVA and storing output in a log file. 
Batch script to be run will take input parameters / arguments.
The below simple Java code will help to run a batch file (bat script) and store the output of batch run in to a file. The batch script to be run from the java takes few parameters as inputs.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

public class ApplicabilityCheck {

    public static void main(String[] args) {
        try {
            System.out.println("ESS BVT");
            String cfgprefix = "server1";
            InetAddress hostname = InetAddress.getLocalHost();
            String host = hostname.getCanonicalHostName();
            String httpsPort = "16311";
            String truststorepassword = "krishna";
            String basicauthuser = "krishna";
            String basicauthpassword = "krishna";
            String essCleintPath = "C:\\Krish\\My Docs\\Foundation services install\\ESS - 15183\\ESSAuthnClientApp\\MyApp\\bin";
            String essOutputDir = "C:\\Krish\\My Docs\\Foundation services install\\ESS - 15183\\ESSAuthnClientApp";
            String passString = "succeeded";
            File tokenOutFile = new File("ESS_Token_Out");
            File essOutputFile = new File(essOutputDir + "\\ESS_OUTPUT.log");
            File iosdpTrustStoreFile = new File(essCleintPath + "\\16311.jks");
            File runExampleFile = new File(essCleintPath+ "\\runExampleApp.bat");
            if (iosdpTrustStoreFile.exists() && runExampleFile.exists()) {
                System.out.println("File:- "
                        + iosdpTrustStoreFile.getAbsolutePath() + " Exists");
                String setupExampleString = "setupExampleApp.bat -cfgprefix "
                        + cfgprefix + " -host " + host + " -port " + httpsPort
                        + " -protocol https -truststorelocation \""
                        + iosdpTrustStoreFile.getAbsolutePath()
                        + "\" -truststorepassword " + truststorepassword
                        + " -basicauthuser " + basicauthuser
                        + " -basicauthpassword " + basicauthpassword;
                System.out.println(setupExampleString);
                String runExampleAppString = "\""
                        + runExampleFile.getAbsolutePath() + "\"  " + cfgprefix
                        + " -tokenOutFile " +  tokenOutFile.getName() + " -uname "
                        + basicauthuser + " -pword " + basicauthpassword;
                System.out.println(runExampleAppString);
                /* Now running the Run Example */
                Runtime runtime = Runtime.getRuntime();
                Process p = runtime.exec(runExampleAppString, null, new File(essCleintPath));
                String line;
                BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
                BufferedWriter w = new BufferedWriter(new FileWriter(essOutputFile));
                while ((line = r.readLine()) != null) {
                    if(line.contains(passString)) System.out.println("ESS installed");
                    w.write(line);
                }
                r.close();
                w.close();

            } else
                System.out.println("Trust Store JKS file is not available");

        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

References:
[1]


1 comments to "Running a batch script from Java(Arguments for Bat file) and output of run in a file"

  • For hottest information you have to visit web and on world-wide-web I found this
    web site as a finest web site for latest updates.
    Take a look at my web site Deux Lux handbag

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree