I'm trying to use ProcessBuilder to load executables from my Java application.
Code:
String [] cmdArray = new String[1];
cmdArray[0] = mPathToFile + mGameArrayList.get(i).Directory + mGameArrayList.get(i).Executable;
ProcessBuilder builder = new ProcessBuilder(cmdArray);
final Process process = builder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
The .exe I'm trying to load is a small game I've made myself and compiled in VS 2010. I've made any errors output to the console which the Java program is picking up with the BufferedReader, the loaded .exe then crashes. I'm getting errors telling me that the game can't find resources like mesh' and textures which leads me to believe that I'm loading it wrong since it works fine by double clicking it to load. Is there another method for executing programs that have resources it requires to run?
Thanks in advance.
The executable probably expects its working directory to be the directory it exists in. By default, according to the ProcessBuilder documentation:
a working directory. The default value is the current working directory of the current process, usually the directory named by the system property user.dir.
Add a line after you construct your ProcessBuilder
to set the current working directory:
builder.directory(new File(mPathToFile + mGameArrayList.get(i).Directory));
(assuming that concatenation gives you the directory the executable is in)