Quantcast
Channel: Imran Tariq's Blog » websphere
Viewing all articles
Browse latest Browse all 4

Run batch file from java and send parameter

$
0
0

I was working on a client program for Websphere where I need to change WebSphere configuration with an executable jar file that will get input from user and change configuration. As WebSphere uses Jython commands in Windows environment so in this example I am calling bat file from java code that will internally call jython file and pass parameters to set WebSphere configurations.

  1. package com.bat;
  2.  
  3. import java.io.BufferedReader;
  4.  
  5. import java.io.IOException;
  6.  
  7. import java.io.InputStream;
  8.  
  9. import java.io.InputStreamReader;
  10.  
  11. public class WindowsScript {
  12.  
  13.   /**
  14.  
  15.    * @param args
  16.  
  17.    * @throws IOException
  18.  
  19.    */
  20.  
  21.   public static void main(String[] args) throws IOException {
  22.  
  23.     Runtime rt = Runtime.getRuntime();
  24.  
  25.     Process process = rt.exec(new String[]{"cmd.exe", "/c", "D:\\test.bat c: \"C:/Program Files/IBM/SDP/runtimes/base_v7/profiles/AppSrv01Secure/bin\" server1 \"a1.rootdir\""});
  26.  
  27.     InputStream inputStream = process.getInputStream();
  28.  
  29.     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  30.  
  31.     String line;
  32.  
  33.     while ((line = bufferedReader.readLine()) != null) {
  34.  
  35.         if(line.contains("value="))
  36.  
  37.           System.out.println(line);
  38.  
  39.     }
  40.  
  41.     System.out.println("end------");
  42.  
  43.   }
  44.  
  45. }

You can get these parameters in batch file as

  1. @ECHO OFF
  2. %1
  3. cd %2
  4. jythonCode.py %3 %4 %5

and pass required parameters to jython file so that jython code can perform its operation

 

Share


Viewing all articles
Browse latest Browse all 4

Trending Articles