File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Execute cmd + bat + command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Execute cmd + bat + command" Watch "Execute cmd + bat + command" New topic
Author

Execute cmd + bat + command

Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 23
Hello guys!

Im having a problem I need to call cmd, excute a bat file external of the proyect and then run a command.

What I do is



And I getting a exit value of 1. I think that the problem could be in this: /c c:\\bat\\my.bat

I also tried to call run.exe with cmd [] with the arry. And get the same problem. Can you help me with this?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Take a look at this article When Runtime.exec() won't. Hope it helps.
Manish Doomra
Greenhorn

Joined: Sep 05, 2008
Posts: 21
Hello Diego,

As far as i understand your requirement, I am providing you a solution based on my understanding.

Your requirement:-
1) Call a batch file from Java program.
2) Capture the exit code of batch file in Java program.

Java Program will go like this:-



Note:- Instead of exitValue(), i have used waitFor()

and batch file contains code like:-



The output i got is after executing the java program :-

exitValue: 1


I hope this will solve your problem

Diego Creador wrote:Hello guys!

Im having a problem I need to call cmd, excute a bat file external of the proyect and then run a command.

What I do is



And I getting a exit value of 1. I think that the problem could be in this: /c c:\\bat\\my.bat

I also tried to call run.exe with cmd [] with the arry. And get the same problem. Can you help me with this?


Manish Doomra
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

As far as I know, the following command

cmd.exe /c c:\bat\my.bat /c mycommand

runs the bat file c:\bat\my.bat and gives it parameters /c mycommand. Add echo %* > c:\bat\test.log to your bat file to check if it is true.

If you want to run two OS commands using cmd.exe, you'll have to invoke it twice, or maybe use command separating operator &&. Only one /c argument is supported on the command line of cmd.exe.

Run cmd.exe /? in a console window and see what is suported on your OS version.
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 23
Hello! Thanks for the help.

I made the changes and I add the log file. And I see the problem, the command that I need to execute is not a paramet of the bat file.

This code should work this way, the bat file set the enviroment and then after the bat excutions end, run the command. Is there a way to do this???

Regards
Diego
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

It is my understanding that you are trying to setup some environment variables before executing 'mycommand'. Am I correct?

If so then is is usually better to execute cmd.exe using Runtime.exec() or ProcessBuilder and write the commands to the Process stdin. Remember to close stdin when you have written all the commands. Note - if you have not read the 4 sections of http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html and implemented all the recommendations then you should.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

You can try this:

File my.bat:Line 3 does the trick: it executes parameters given to the bat file as an OS command.

Then run in the console window:

It should output the text "Hallo!" on the console.

If the command you need to execute contains quotes, you may need some tweaking to get it right, but basically this sould do it.

Another way: create the bat file dynamically as needed (initialization + the command), run it and then delete it (or using stdin as James suggested, but bear in mind that some command might have different syntax when used on the command line or in a bat file, eg. a for command).

If your initialization is just environment variables, this should be doable directly from Java (but I've no experience with this). That way you could run the command directly and avoid batch files altogether.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Martin Vajsar wrote:
Another way: create the bat file dynamically as needed (initialization + the command), run it and then delete it (or using stdin as James suggested, but bear in mind that some command might have different syntax when used on the command line or in a bat file, eg. a for command).


Annoying but true. I fail to understand why MS had to modify the syntax for this when all other shells that I have worked with keep exactly the same syntax no matter how the script is supplied.


If your initialization is just environment variables, this should be doable directly from Java (but I've no experience with this). That way you could run the command directly and avoid batch files altogether.


It is easy enough to do using Runtime.exec() by passing the environment as the second parameter and null as the third BUT one normally has to manually copy over the original environment. The whole process is much easier with ProcessBuilder since one can add directly to the set of environment variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute cmd + bat + command
 
Similar Threads
problem of brackets running file bat
start weblogic server in Java code
Destroying a windows process
how to open the excel file in java program?
Running a .bat/ .cmd file from Java