0

I am facing a problem with a .bat file run from Java web application.

Current setup in local machine: create web application and method contains

Process p = Runtime.getRuntime().exec("c:/test/myFile.bat");

When I run above code, its showing file not found.

But if I copied the .bat file to tomcat bin folder it's working fine. But I need to execute the bat file in my separate folder, I even set the class path also, but I'm not able to execute the bat.

Screen 1:

I have placed 5 files in Tomcat root directory din folder, as well as placed at c:/psgsscripts/--> folder also (check the Java code; only bat file is taken other .tbc files are looking at Tomcat bin folder location at run time)

Screen 2:

bat file contains tclsh psg.tbc %1

Screen 3:

web application java code in class method( newjobid is the parameter for .bat file)

Screen 4:

If I placed it all .bat file and .tbc files bin folder it's working fine (Java code represent read the .bat file from c drive but other .tbc files look at Tomcat bin folder at run time)

but requirement is those files are placed at other drives (other than Tomcat folder) like C or D or E drivers.

Screen1

Screen2

Screen 3

Screen 4

halfer
  • 19,824
  • 17
  • 99
  • 186
Suresh
  • 49
  • 1
  • 6
  • 3
    possible duplicate http://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application – karman Nov 03 '16 at 13:54
  • It looks like screens 2, 3 and 4 would be better as text rather than screenshots. Would you replace them please, by editing the question? – halfer Nov 11 '16 at 16:48

2 Answers2

2

You need to execute the command to run the batch file like below

Runtime.getRuntime().exec("cmd /c start c:\\test\\myFile.bat");
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
  • Thanks for ur valuable response, but in my case, when ever i run the web application cmp prompt is showing ... how can i restrict that (need to close cmp prompt), Internally bat file call other .tbc files( this files also placed at same "c:/test" folder but it not identified. but its working perfectly in when ever i placed the ".tbc" files in "tomcat/bin" folder, but i dont want to placed the files in bin folder. please help me on this issue – Suresh Nov 07 '16 at 18:39
  • Thanks for ur valuable response, but in my case, when ever i run the web application cmp prompt is showing ... how can i restrict that (need to close cmp prompt), Internally bat file call other .tbc files( this files also placed at same "c:/test" folder but it not identified. but its working perfectly in when ever i placed the ".tbc" files in "tomcat/bin" folder, but i dont want to placed the files in bin folder. please help me on this issue – Suresh Nov 07 '16 at 18:47
2

Try the following:

Process p = Runtime.getRuntime().exec("cmd /C start c:/test/myFile.bat");

If you look at the java docs, the method exec expects an OS command and not the file name. So in the proposed solution, "cmd" is the OS command, /C is a switch that tells the OS command to carry out the command specified by string and then terminate. Here the command specified by the string is start. The command "start" requires a file name with full path as its parameter.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • Thanks for ur valuable response, but in my case, when ever i run the web application cmp prompt is showing ... how can i restrict that (need to close cmp prompt), Internally bat file call other .tbc files( this files also placed at same "c:/test" folder but it not identified. but its working perfectly in when ever i placed the ".tbc" files in "tomcat/bin" folder, but i dont want to placed the files in bin folder. please help me on this issue – Suresh Nov 07 '16 at 18:39
  • .bat file contains "tclsh psg.tbc %1" (its single line code only, here %1 is input param from java code) but run time jvm look the .tbc file in tomcat bin folder only, how can i change that? – Suresh Nov 07 '16 at 18:47
  • What do you mean by `cmp prompt`? Can you share a screenshot of it? Also, can you share the portion of your batch file that try to call the *.tbc files? – VHS Nov 07 '16 at 19:14
  • Hi bro. can you please check the attached screens and problem description. please respond asap. – Suresh Nov 09 '16 at 19:27
  • Give full path to the tbc file. Update your batch file to `tclsh c:\psgscripts\psg.tbc %1`. – VHS Nov 09 '16 at 19:41
  • Bro, not showing any output. if possible can u pls drop ur mobile no to my email, i will call you and expalin problem--> narayanasuresh86@gmail.com – Suresh Nov 10 '16 at 06:48