Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execute cmd + bat + command

 
Diego Creador
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this article When Runtime.exec() won't. Hope it helps.
 
Manish Doomra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic