我需要做的是获取正在运行的jar/exe文件的名称(它将是Windows上的EXE,mac/linux上的jar).我一直在寻找,我似乎无法找到如何.
如何获得运行Jar或Exe的名称?
文件(MyClass.class.getProtectionDomain()getCodeSource()的getLocation()toURI().);
返回已编译应用程序中的简单路径,并在jar中出错:
URI不是分层的
我的解决方案是:
private File getJarFile() throws FileNotFoundException {
String path = Main.class.getResource(Main.class.getSimpleName() + ".class").getFile();
if(path.startsWith("/")) {
throw new FileNotFoundException("This is not a jar file: \n" + path);
}
path = ClassLoader.getSystemClassLoader().getResource(path).getFile();
return new File(path.substring(0, path.lastIndexOf('!')));
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助你,我测试代码,这将返回完整的路径和名称.
也许你想用代码玩一点,给我一些反馈.
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());?
Run Code Online (Sandbox Code Playgroud)
这是在stackoverflow上的类似但不是==问题上 找到的如何获取正在运行的JAR文件的路径?
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().g??etPath());
Run Code Online (Sandbox Code Playgroud)
应该把罐子给你。
至于 exe,因为我假设您正在使用某种包装器,所以在运行之前您需要知道 exe 的名称。然后你可以使用类似的东西:
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13981 次 |
| 最近记录: |