I have a java project which executes a batch file. I would like to know if it is possible to change a normal java project to a spring boot project. If it is, can i know how do i roughly go about doing it?
I have read what is spring boot. However i am totally confused as to how do i apply that into this task of a java class executing a batch file.
Here are my codes:
RunBatchMain.java
public static void main(String args[])
{
//Run batch file using java
String filePath = "C:/Users/attsuap1/Desktop/test.bat";
try {
Process p = Runtime.getRuntime().exec(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
GetResponseMain.java
public static void main(String args[])
{
String filePath = "C:/Users/attsuap1/Desktop/test.bat";
try {
Process p = Runtime.getRuntime().exec(filePath);
p.waitFor();
InputStream in = p.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = -1;
while((c = in.read()) != -1){
baos.write(c);
}
String response = new String(baos.toByteArray());
System.out.println("Response From Batch File : "+response);
int exitVal = p.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Exception e) {
e.printStackTrace();
}
}
Someone please help me. Thank you so much.
Yes it's possible. You can start by going to Spring Initializr page. You can generate a starter project there. Download the generated project then import to your IDE. The starter project contains a sample class with a main method. You can simply copy paste your code there as a start.
Yes, you can convert your project into spring boot in two ways.
Through Spring initializr(https://start.spring.io/). Go through the steps there, download a zip file and finally import project into your favorite IDE and copy your existing code there.
Through your IDE also you can create Spring boot project and copy paste your existing code, that's it. Please refer below on how to create spring boot project from your STS IDE.

Since it's a normal java project, no need to add any special dependencies in maven pom.xml file, it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With