• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

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
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);
 }
 }

Open in new window

0
yuk99
Asked:
yuk99
  • 4
  • 4
1 Solution
 
CEHJCommented:
Try it with File.createTempFile instead
0
 
yuk99Author Commented:
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.
0
 
CEHJCommented:
>>but my filename is fixed - startup.m

Sorry - didn't know that
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
... 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
0
 
yuk99Author Commented:
OK, I think I found the problem, but it's not Java related.
On mac/unix matlab is executed by symbolic link. But on windows, there is a small file matlab.exe, which runs another big matlab.exe in win32 folder. If I run the second file directly from the java program, everything is ok. It looks like when the first process stops and before the second process is ready to accept startup.m, it's get deleted by terminated jvm.
Have no solution yet, but will probably just leave the file without deleting it. Or will try to delete it by matlab at the end of the script.
0
 
yuk99Author Commented:
CEHJ,
The file is not in the temporary directory, but in the current directory, where matlab is running from.
0
 
CEHJCommented:
>>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?
0
 
yuk99Author Commented:
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now