• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

runtime.exec problems

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't get runtime.exec to execute a program in Java. I am trying to execute a bat file. I read that in order to run bat files as opposed to .exe files the runtime command has to include "cmd /c start" before the name of the bat file. I have run bat files successfully this way before.

The command that I am running works when typed from the command line. It is:

z:/n4/pkg/MrServers/MrVista/Simu/StartSimEnv.bat -AutoStart GOLD_256x512.dat

When I try the following code:

File file = new File(imageDir);
cmd = "C:\WINDOWS\system32\cmd /c start "+simDir+"StartSimEnv.bat -AutoStart " + imageName;

IJ.log("cmd = " + cmd);

Runtime runtime = Runtime.getRuntime();

try{
Process proc = runtime.exec(cmd,null,file);
}
catch(Exception e)
{
IJ.log(e.toString());
}

StartSimEnv.bat file gives a message about the usage being wrong as if I'm giving the wrong parameters. If I delete imageName from the cmd string it doesn't complain about usage but doesn't do the task I want it to do.

I tried putting the parameters in an array as follows:


File file = new File(imageDir);
String[] cmdArray = {cmdPath, "/c", "start",simDir+"StartSimEnv.bat","-AutoStart",imageName};

IJ.log("cmdArray = " + cmdArray[0] + " " + cmdArray[1] + " " + cmdArray[2] + " " + cmdArray[3] + " " + cmdArray[4] + " " + cmdArray[5]);
Runtime runtime = Runtime.getRuntime();

try{
Process proc = runtime.exec(cmdArray,null,file);
}
catch(Exception e)
{
IJ.log(e.toString());
}

I still get a message about usage.
I have made efforts with process builder also but can't get that to work either. Does anyone have any ideas why this isn't working?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runtime.exec isn't easy to get right; read this article to understand the issues.
 
Gamaliel Isaac
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf I read that article already but it didn't help. Thanks anyway.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, since you didn't tell us the error message you got, there's not much we can do to help.

One of things that article teaches is that you should definitely be using the method that takes a String[], instead of the one that takes a String parameter, so the second approach is to be preferred.
 
Gamaliel Isaac
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only message I got was regarding usage and I know my usage is correct because it works on the command line.


DEBUG_MODE=d|' ' (current: )

Usage: StartSimEnv [-h [env] -D -T -d -config -close [forced] -outFil -remote -A
utoStart -w <sec> -noPause -rd]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gamaliel Isaac wrote: I know my usage is correct because it works on the command line.



That is a very bad assumption to make -- especially since the executable is telling you that the usage is wrong. Even different command shells behave differently with user input from the command line.

I would recommend modifying the batch file to print out what it will run before it actually run it (adding a single echo should do it). This way you can see the differences in how the executable is being run from the batch file in both cases.

Henry
 
Gamaliel Isaac
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, I commented out echo off in the bat file and still got the same error message without more text. It's possible that the reason for that is that the bat file is calling another file. The file is complicated and hard to understand.

I did try something else I called the bat file with all it's parameters inside another bat file and then called that bat file from runtime.exec() That is working now. To use this approach I will have to use java to create a bat file each time containing the parameters I want.

Even though this is working I am very confused about this.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gamaliel Isaac wrote:
Even though this is working I am very confused about this.



Gamaliel Isaac wrote:Ulf I read that article already but it didn't help. Thanks anyway.



There is nothing in the code you have published to indicate that you have read the 'traps' article. You certainly do not seem to have implemented most of the recommendations.
 
Gamaliel Isaac
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did look at it and I didn't test all its recommendations because I didn't think they would help but I'll take another look
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gamaliel Isaac wrote:I did look at it and I didn't test all its recommendations because I didn't think they would help but I'll take another look



Big mistake on your part. Implementing the recommendations may not make any difference but without them your code is highly suspect. The diagnostic information they provide may tell you exactly what the problem is.
 
Gamaliel Isaac
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was a big mistake on my part. I used the method described in the article and it worked.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gamaliel Isaac wrote:It was a big mistake on my part. I used the method described in the article and it worked.



I'm pleased you sorted it.
 
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic