Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call/control .exe file not in current directory

 
Tony Tranquil
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a problem when I use a class file to call an .exe file which is not in the current folder the class is in. For example, a A.exe file is in such folder like below. A.exe uses some imgaes files using relative path. And caller.class in other foler.
c:\
|----app
| |
| |----A.exe
| |
| |----images
| |
| |----images.jpg
|
|----javaapps
| |
| |-----caller.class
When I run caller.class file in javaapps folder, I got error message which told me A.exe file can not find image file. If put A.exe and images folder with image file in javaapps folder, caller.class works fine.
Could anybody please tell what's wrong, how to solve this problem?
Thanks
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony
Try to write a batch file (as you seem to be on Windows) that runs A.exe. In the batch file you write the following,
test.bat
---------
cd c:\app
A.exe
and see if it works...
The problem here is from Java we can't "cd" directory you know and if we execute the way you are trying (from javapps folder via the class file) then it wouldn't find "images" as in A.exe images are referred relatively (to apps folder)...
See if it works..
Regards
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, forgot to mention that we should try running this batch file from the java code instead of the A.exe directly....we can put this batch anywhere as far as our java code writes correct path to it...
Regards
Maulin
 
Tony Tranquil
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maulin,
First, Thank you for your reply. Following your suggestion, the program still doesnt work well, the Caller class got return value 1, but A.exe can not run. I post my source code below, please help me have a look.
Thanks alot.
Tony
public class Caller {
public static void main(String[] args) {
try{

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("C:\\app\\go.bat");
int exitVal = proc.waitFor();
System.out.println("Process exit value: " + exitVal);
}catch(Throwable t){
System.out.println(t.getMessage());
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic