1

I'm trying to get an application (a game) to start with a Java script. There is a long explanation behind why, so I'll skip that part.

The game won't run unless it is executed from its own directory, IE: Just telling Java to launch the EXE gives errors within the game. It must be launched from its directory.

I've Googled for hours over this and can't seem to find any real answers. After a lot of Googleing, I've learned this should be right:

String workingDir = "F:\\Games\\COD4\\";
String cmd = "iw3mp.exe";           

Runtime.getRuntime().exec(cmd,null,new File(workingDir));

However, I get this error:

Exception in thread "main" java.io.IOException: Cannot run program "iw3mp.exe" (in directory "F:\Games\COD4"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at cod4launcher.main(cod4launcher.java:29)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 4 more

Please help! Thanks.

2 Answers 2

6

Try this instead:

String workingDir = "F:\\Games\\COD4\\";
String cmd = workingDir + "iw3mp.exe";
Runtime.getRuntime().exec(cmd,null,new File(workingDir));

iw3mp.exe probably isn't on your system path so you have to give Java an absolute path to the executable.

0

Use java.lang.ProcessBuilder#directory(java.io.File)

Processs p = 
    new ProcessBuilder(cmd).
        directory(new File(workingDir)).
        start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.