gpt4 book ai didi

java - 导出处清洁问题

转载 作者:行者123 更新时间:2023-12-02 03:55:13 24 4
gpt4 key购买 nike



我正在使用 JSmooth 进行 JAVA 工作。
我看到每次启动时都会创建一个 tmp JAR。

创建的 tmp jar 文件大约为 10 Mb。

当我退出 JSmooth 构建的可执行 JAVA 应用程序时,
每次启动时都会保留临时 jar 文件。

我阅读了以下有关 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.


我尝试在导出处删除 jar:

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

我的 JSmooth 文件:

<?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>

我认为计算机的可用空间非常危险(每次启动时,可用空间都会减少 10 Mb)因为计算机可能会出现故障。

希望我的问题是准确的,不会无缘无故打扰别人。
我非常感谢那些能够抽出时间回答的人。

最佳答案

Java 无法删除 Windows 下当前正在使用的文件。这是 Windows 的限制。

这意味着任何通过 file.delete()file.deleteOnExit() 从程序中删除它们的尝试都将失败。这也是JSmooth出现bug,无法删除文件的原因。

虽然您无法直接删除该文件,但您可以启动一个辅助程序(例如 cmd)来安排在 10 秒左右之后删除该文件,这可以通过在退出程序时运行以下命令来完成:

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

因为 Windows 没有适当的命令行 sleep 命令,所以我滥用 ping 命令在运行下一个命令之前给予 3 秒的延迟。此技巧的解释如下:How to sleep for 5 seconds in Windows's Command Prompt? (or DOS)

关于java - 导出处清洁问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com