0

So I have a program that runs something like the following

public class SHandler extends Handler { 
    File lmpFile;

Down later in the program:

lmpFile = new File("Stuff.zip"); // This should create a file called "stuff.zip" in the present directory
OutputStream fos = new FileOutputStream(lmpFile); // Fill the file with whatever

Then from my main I call

 S.SHandler SpecialSH = new S.SHandler(args);
 //use the object for whatever
 SpecialSH.delFile();

Delfile is made like this and is a method inside the class:

public void delFile() {
    lmpFile.deleteOnExit();
    lmpXMLFile.deleteOnExit();
}

To my knowledge this program works right on my local machine (Windows 7 Enterprise), however on our development box when I run this it's tossing a LOT of files that the program pulls all over the place. The execution path is /usr/data/dev/Handler and it's putting "stuff.zip" (and the files extracted from it) in /etc/cron.d and despite trying to remove them I am unable to.

Note This program is being called via a bash script which is invoked by a cron job on a machine running RHEL6. Anyone able to help this would get my undying love and appreciation.

Edit: The bash script is simply:

export JAVA_HOME=/usr/data/java/current
export PATH=$JAVA_HOME/bin:$PATH

/usr/data/java/current/bin/java -jar /usr/data/dev/Handler/Handler.jar

Tl;DR: File runs fine on windows, when RHEL6 calls a cron, files end up where they shouldn't. How can I make my program handle this?

jordanm
  • 33,009
  • 7
  • 61
  • 76
A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • So, is the problem that files are created in an unexpected place, or that they aren't being deleted when the program exits? – Alex Aug 22 '12 at 17:39

2 Answers2

2

It looks like the working directory is /etc/cron.d/ (executable path is different).

Relative paths (when using java.io.File), are relative to the working directory. If you want your files placed in a different directory, use absolute file paths: /path/to/stuff.zip (note the leading slash).

Community
  • 1
  • 1
EthanB
  • 4,239
  • 1
  • 28
  • 46
  • 2
    You could also add `cd desired-directory` to the bash script, just before the call to the Java program. – chepner Aug 22 '12 at 17:31
  • Or set an environment variable on each system and retrieve it in the Java code and append to the file name as the path. – Dan Aug 22 '12 at 17:35
  • @Damien.Bell, is there a reason you're not using `File.Delete()`? – EthanB Aug 22 '12 at 18:33
  • I can use file.delete, but there's times where it calls before I wanted it to, deleteonexit() ensures that when the program closes it's done. – A_Elric Aug 22 '12 at 19:44
  • @Damien.Bell Does changing directories result in the files being deleted afterwards? – EthanB Aug 22 '12 at 19:50
  • I'll follow this up with an answer in just a moment if it does, I'm changing the cron now. – A_Elric Aug 23 '12 at 12:56
0

Perhaps these files are not closed, when delete occurs, or perhaps, an other program use them ?

Istao
  • 7,425
  • 6
  • 32
  • 39