Reputation: 673
What I need to do is get the name of the running jar/exe file (it would be an EXE on windows, jar on mac/linux). I have been searching around and I can't seem to find out how.
How to get name of running Jar or Exe?
Upvotes: 11
Views: 19063
Reputation: 101
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
returns simple path in the compiled application and an error in jar:
URI is not hierarchical
My solution is:
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('!')));
}
Upvotes: 10
Reputation: 455
Using Lunch4j you can add an image name for exe file by editing generated xml, you have to change tag value true from false to true
Upvotes: -1
Reputation: 1007
Hope this can help you, I test the code and this return you the full path and the name.
Maybe you want to play a little more with the code and give me some feed back.
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
This was found on a similar but not == question on stackoverflow How to get the path of a running JAR file?
Upvotes: 10
Reputation: 2473
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
Should give you the jar.
as for the exe, as I'm assuming you're using some sort of wrapper, you'll need to know the name of the exe before it's run. Then you could use something like :
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
Upvotes: 5