This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Not able to call a bat file from JAVA

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to call a .bat file from Java.
The .bat file works perfectly if I am running it manually.

Now I need it to be called from Java. But it shows a blank console window, without executing the .bat file (it is not able to load the .bat file).

Here is the code:



Please suggest what is wrong in my code.
Thanks in advance
 
Sheriff
Posts: 22662
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try removing the quotes around your path. Although it is usually necessary if you type the command as one whole, this method should remove that need. Remember, you're not passing the file as an entire parameter, but the file wrapped in quotes.

Consider the following:

When run from D:, the results are as follows:
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are calling a bat file, the you should not be using Java......

You should rethink what you are doing...

While it can be done...

SHOULD IT???
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Fahlbusch:
If you are calling a bat file, the you should not be using Java......



We know precious little about Praveen's situation to pass such a judgment. If he wants to invoke a batch file from java, let's help him. If he asks if it is a good idea or not and fills in details about what he's dealing with, then we can help him make an informed decision.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably want to add

System.out.println(SystemFilesDir.getAbsolutePath()+"\\Batch.bat")

to see what it actually looks like. And it's entirely possible that those extra quotes are causing the problem; Windows CMD is really funny about quotes.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the JavaDocs on java.lang.Process

Note the critical point about consuming the standard out and standard err output streams. If you don't, it just sits there.

This has come up a LOT on this forum.

Bill
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also you need to read this article from JavaWorld

regards,

Geo
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this...then read through what is going on...should help...


[ September 08, 2008: Message edited by: G Estes ]
reply
    Bookmark Topic Watch Topic
  • New Topic