yuk99
asked on
deleteOnExit issue
Hello,
I've got a small program in Java. It's a pretty simple wrapper to run Matlab. It creates temorary file startup.m in current directory with all the matlab commands and then runs matlab. At the end of the program this files supposed to be deleted with deleteOnExit(). It runs perfectly fine on Mac and Unix with java 1.5.x. But on Windows (I tested it with java 1.6, so it may be some version issue) the file is generated and deleted before running matlab. I show some part of the code below.
The code will run on server and supposed to be platform independent. Please explain what wrong with it either on Windows or with newer java. Can you suggest the best way to delete this temporary file at the end of the program without altering the whole program a lot? I'm not an expert in Java.
Thanks
I've got a small program in Java. It's a pretty simple wrapper to run Matlab. It creates temorary file startup.m in current directory with all the matlab commands and then runs matlab. At the end of the program this files supposed to be deleted with deleteOnExit(). It runs perfectly fine on Mac and Unix with java 1.5.x. But on Windows (I tested it with java 1.6, so it may be some version issue) the file is generated and deleted before running matlab. I show some part of the code below.
The code will run on server and supposed to be platform independent. Please explain what wrong with it either on Windows or with newer java. Can you suggest the best way to delete this temporary file at the end of the program without altering the whole program a lot? I'm not an expert in Java.
Thanks
import java.io.*;
public class RunMatlab {
public static void main(String[] args) throws Exception {
createStartupFile(args);
String[] cmdArray = new String[3];
cmdArray[0] = "matlab";
cmdArray[1] = "-nosplash";
cmdArray[2] = "-nodisplay";
Process p = Runtime.getRuntime().exec(cmdArray);
...
p.waitFor();
}
protected static void createStartupFile(String[] args){
try {
File startupM = new File("startup.m");
BufferedWriter bw = new BufferedWriter(new FileWriter(startupM));
...
bw.write("quit();");
bw.flush();
bw.close();
startupM.deleteOnExit();
} catch (IOException e){
e.printStackTrace();
System.err.println("Could not create startup.m file to execute. Exitting.");
System.exit(999);
}
}
Try it with File.createTempFile instead
ASKER
CEHJ,
I don't think it's right. I don't have problem creating the file. createTempFile generates the filename, but my filename is fixed - startup.m. In addition it also requires using deleteOnExit method. In my case for some reason it doesn't work. No, it works, but it deletes the file before running matlab.
I don't think it's right. I don't have problem creating the file. createTempFile generates the filename, but my filename is fixed - startup.m. In addition it also requires using deleteOnExit method. In my case for some reason it doesn't work. No, it works, but it deletes the file before running matlab.
>>but my filename is fixed - startup.m
Sorry - didn't know that
Sorry - didn't know that
... although i'm not sure *why* it's fixed. If it's in the temp directory you don't need to worry about deleting it at all
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CEHJ,
The file is not in the temporary directory, but in the current directory, where matlab is running from.
The file is not in the temporary directory, but in the current directory, where matlab is running from.
>>The file is not in the temporary directory, but in the current directory,
Yes but you create it and write to it from scratch, so why not put it where it can be deleted in due course?
Yes but you create it and write to it from scratch, so why not put it where it can be deleted in due course?
ASKER
It's a special file for Matlab. It runs automatically when Matlab started. It should be either in a current directory or in a path.