0



I am working on JAVA with JSmooth.
I see at each launching, a tmp JAR is created.

Created tmp jar files are approximately 10 Mb.

As I quit an executable JAVA application built by JSmooth,
the temp jar files remains at each launching.

I read the following topic about JSmooth:
JSmooth - exit application

7.1.2.
What happens to the extracted jar file when the java application exits?

Whenever possible, the wrappers delete the file on exit. Note however that this is not always possible. For example, an instance of a JVM created using the JVM DLL does not unload cleanly. This behaviour (not to call it a bug) is documented by Sun, and there is no known work-around for it.

Both Windowed and Console wrapper always prefer the JVM instanciation methods that allow them to delete the temp jar after the application exits. Note that embedding a jar in the exe is not required, it is always a safe choice to just leave it on the filesystem near the executable, either in the same directory, or in a sibling or parent directory.

Note also that deleting the files in the windows TEMP directory is not required for Windows applications, and most application just leave them, letting the operating system manage it. The standard behaviour of MS Windows is to propose the user to delete the temporary files when the disks are running low on available space.


I tried to delete the jar at the exit:

//For matching convention, I wrote the file in a not null package
package main;

import java.io.File;

//It is an utility class, so the class is final
public final class Constants {

    //The tmp folder key
    private static final String TMP_FOLDER = "java.io.tmpdir";

    //The main class
    private static Class<?> _mainClass_ = Constants.class;

    //It is an utility class, so hide the constructor
    private Constants(){
    }

    public static void main(String[] _args) {
        //Processing
        //....
        exit();
    }

    /**@see System#exit(int)*/
    public static void exit() {
        String path_ =_mainClass_.getProtectionDomain().getCodeSource().getLocation().getPath();
        //For keeping portability OS, replace all back slashes by slashes
        path_ = new File(path_).getAbsolutePath().replace("\\", "/");

        //For keeping portability OS, replace all back slashes by slashes
        String folder_ = System.getProperty(TMP_FOLDER).replace("\\", "/");

        if (path_.startsWith(folder_)) {
            //If the jar is located in the temp folder, then delete it before exit.
            System.out.println("path: "+path_);
            System.out.println("folder: "+folder_);

            //Try remove temp jar
            new File(path_).delete();
            new File(path_).deleteOnExit();
        }
        //Exit the program
        System.exit(0);
    }
}

My JSmooth file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<jsmoothproject>
    <JVMSearchPath>exepath</JVMSearchPath>
    <JVMSearchPath>registry</JVMSearchPath>
    <JVMSearchPath>javahome</JVMSearchPath>
    <JVMSearchPath>jrepath</JVMSearchPath>
    <JVMSearchPath>jdkpath</JVMSearchPath>
    <JVMSearchPath>jview</JVMSearchPath>
    <arguments></arguments>
    <bundledJVMPath>jre</bundledJVMPath>
    <currentDirectory>${EXECUTABLEPATH}</currentDirectory>
    <embeddedJar>false</embeddedJar>
    <executableName>Test.exe</executableName>
    <initialMemoryHeap>-1</initialMemoryHeap>
    <jarLocation>target\Test.jar</jarLocation>
    <mainClassName>main.Constants</mainClassName>
    <maximumMemoryHeap>1073741824</maximumMemoryHeap>
    <maximumVersion>1.7</maximumVersion>
    <minimumVersion>1.7</minimumVersion>
    <skeletonName>Windowed Wrapper</skeletonName>
    <skeletonProperties>
        <key>Message</key>
        <value>Java has not been found on your computer. Do you want to download it?</value>
    </skeletonProperties>
    <skeletonProperties>
        <key>URL</key>
        <value>http://www.java.com</value>
    </skeletonProperties>
    <skeletonProperties>
        <key>SingleProcess</key>
        <value>0</value>
    </skeletonProperties>
    <skeletonProperties>
        <key>SingleInstance</key>
        <value>0</value>
    </skeletonProperties>
    <skeletonProperties>
        <key>JniSmooth</key>
        <value>0</value>
    </skeletonProperties>
    <skeletonProperties>
        <key>Debug</key>
        <value>1</value>
    </skeletonProperties>
</jsmoothproject>

I think it is very dangerous about available space (at each launching, free space decreases by 10 Mb) for the computer because the computer can be out of order.

I hope, my question is precise and I do not disturbe people for nothing.
I thank very much people who would be able to make time to answer.

A. Ocannaille
  • 175
  • 1
  • 11
cardman
  • 79
  • 3
  • 8
  • 1
    What happens when you try to delete it? – Ferrybig Feb 22 '16 at 15:24
  • @Ferrybig: the tmp Jar files are not deleted. No exception is thrown. Note that the names of tmp Jar files are formatted by temp.jar. Ex: temp1.jar. A beginner user does not know that not only a tmp Jar file is created in the temp folder, but also the tmp Jar file remains in the temp File. It is hard for beginner users to know that they must delete tmp jar files after execution. – cardman Feb 22 '16 at 15:34
  • 1
    You could instruct Windows to delete those files at least on reboot by using PendigFileRenameOperations. Or you try to delete those from previous runs on every new start. – Marged Feb 22 '16 at 15:52

2 Answers2

2

Java cannot delete a file that is currently in use under Windows. This is a limitation of Windows.

This means that any attempt to delete them from your program, either via file.delete() or file.deleteOnExit() is going to fail. This is also the cause of the bug in JSmooth, and why it cannot delete the file.

While you cannot directly delete the file, you are able to start a side program like cmd to schedule the deletion of the file 10 seconds or so later, this can be done by running the following when exiting your program:

new ProcessBuilder("cmd","/c","ping 127.0.0.1 && del "+filename).inheritIO().start();

Because Windows doesn't have a proper sleep command for the command line, I am abusing ping command to give a 3 second delay before running the next command. This trick is explained at: How to sleep for 5 seconds in Windows's Command Prompt? (or DOS)

Ferrybig
  • 15,951
  • 6
  • 51
  • 69
0

@Ferrybig:
Finally,

I delete all previous tmp jar files,
because it is very rare that a tmp jar file is written by a program in the temp file.

Besides, if a tmp jar file is used, deleting it fails so deleting at the end of an execution is safe.

My new code:

//For matching convention, I wrote the file in a not null package
package main;

import java.io.File;

//It is an utility class, so the class is final
public final class Constants {

    //The tmp folder key
    private static final String TMP_FOLDER = "java.io.tmpdir";

    //The main class
    private static Class<?> _mainClass_ = Constants.class;

    //It is an utility class, so hide the constructor
    private Constants(){
    }

    public static void main(String[] _args) {
        //Processing
        //....
        exit();
    }

    /**@see System#exit(int)*/
    public static void exit() {
        String path_ =_mainClass_.getProtectionDomain().getCodeSource().getLocation().getPath();
        //For keeping portability OS, replace all back slashes by slashes
        path_ = new File(path_).getAbsolutePath().replace("\\", "/");

        //For keeping portability OS, replace all back slashes by slashes
        String folder_ = System.getProperty(TMP_FOLDER).replace("\\", "/");

        if (path_.startsWith(folder_)) {
            //If the jar is located in the temp folder, then delete all previous jars before exit.
            //Begin of my new code
            try {
                for (File f: new File(folder_).listFiles()) {
                    if (!f.getName().toLowerCase().endsWith(".jar")) {
                        continue;
                    }
                    f.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //End of my new code
        }
        //Exit the program
        System.exit(0);
    }
}


I thank Ferrybig and Marged for having helped me.

cardman
  • 79
  • 3
  • 8