-2

Possible Duplicate:
How do I run a batch file from my Java Application?

Yes, I know this question has been asked many times. I have seen the solutions but non of them work for me. Currently I am doing this:

public static void main(String args[])
        throws IOException
{
    Runtime rt = Runtime.getRuntime();
    rt.exec("C:\\sample-win32\\sample.bat");    
}

Surprising part is eclipse run smoothly, no error or exception comes. But the file 'sample.bat' does not do what it is supposed to do.Please help.

Say this is my batch file. I am simply making a folder and opening it. When I double click the batch file it works fine. But when I use the java program, nothing happens.

md 1

start 1
Community
  • 1
  • 1
  • 4
    can You post `sample.bat` file here? – Parth Soni Nov 08 '12 at 08:35
  • 1
    are you using relative paths inside the bat file or absolute? – Nikolay Kuznetsov Nov 08 '12 at 08:35
  • Your java code is correct. Problem with `bat` file – Maxim Shoustin Nov 08 '12 at 08:37
  • I'm with @codeMaker here it sounds from the question that the problem is likely in the bat file and not in the overall running of the file from java, so maybe not exactly a duplicate until proven that the bat file isn't the problem. – JRSofty Nov 08 '12 at 08:38
  • 1
    Dude! your code is working perfectly fine. It's absolutely flawless. I wonder what you are doing in the bat file. Could you please throw some light and expose the contents of your sample.bat? – dharam Nov 08 '12 at 08:41

2 Answers2

0

You may have problems with paths in the batch file. Either use absolute paths only or add a cd command at the beginning of your script to jump to a defined folder.

You may expect output on the console but don't see any. This is because you have to redirect the stream. See this question and Brian's answer for a solution.

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

just keep in mind that passing something to the exec method is same as typing in run or in command

Sujan Shrestha
  • 191
  • 1
  • 4