获取运行Jar或Exe的名称
我需要做的是获取正在运行的jar / exe文件的名称(它将是Windows上的EXE,mac / linux上的jar)。 我一直在寻找,我似乎无法找到如何。
如何获得运行Jar或Exe的名称?
希望这可以帮助你,我测试代码,这将返回完整的路径和名称。
也许你想用代码玩一点,给我一些反馈。
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
这是在stackoverflow上的类似但不是==问题上找到的如何获取正在运行的JAR文件的路径?
文件(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('!'))); }
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
应该给你jar子。
至于exe,因为我假设你正在使用某种包装器,你需要在运行之前知道exe的名称。 然后你可以使用类似的东西:
Process p = Runtime.getRuntime().exec (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
使用Lunch4j,您可以通过编辑生成的xml为exe文件添加图像名称,您必须将标记值true从false更改为true