我需要做的是获取正在运行的jar/exe文件的名称(在windows上是EXE,在mac/linux上是jar )。我一直在寻找,但我似乎找不到方法。
如何获取运行Jar或Exe的名称?
发布于 2013-01-07 10:46:57
希望这能帮助你,我测试了代码,这将返回完整的路径和名称。
也许你想在代码上多玩一点,然后给我一些反馈。
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());这是在stackoverflow How to get the path of a running JAR file?上的一个类似但不是==的问题上发现的
发布于 2013-09-27 15:50:01
File(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('!')));
}发布于 2013-01-07 10:47:22
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); 应该会把罐子给你。
至于exe,我假设你正在使用某种包装器,在运行它之前,你需要知道exe的名称。然后你可以使用类似这样的东西:
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");https://stackoverflow.com/questions/14189162
复制相似问题