11

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1947236
  • 673
  • 3
  • 12
  • 27

5 Answers5

10

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?

Community
  • 1
  • 1
  • *"give me some feed back."* Fails for applets and anything launched using JWS. Usually a bad strategy to implement a good idea. Until the OP mentions what the idea is, we are guessing about the best way to help. – Andrew Thompson Jan 07 '13 at 02:50
  • Hi I managed to use this approach to get the path, but it's in .target/classes folder, but I want to read the Manifest file in another folder under target folder, I'm new to Java, is there any way to do this? Many thanks. – wawawa Dec 04 '19 at 10:29
10

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('!')));
}
Alexander
  • 101
  • 2
  • 4
  • I've been trying all evening to find something that works. This saved me!! Thank you! I'm not sure why, but `class.getProtectionDomain().getCodeSource().getLocation(),` and everything else for that matter, was returning `rsrc:.`. – Cardinal System Jan 24 '19 at 02:06
5
  File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().g‌​etPath());  

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");
Mark
  • 2,423
  • 4
  • 24
  • 40
  • Join us in [post 2004](http://en.wikipedia.org/wiki/Java_version_history#J2SE_5.0_.28September_30.2C_2004.29) and use `ProcessBuilder` to create the `Process`. – Andrew Thompson Jan 07 '13 at 02:51
1

Try the following

System.getProperty("java.class.path")
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
  • 1
    Welcome to StackOverflow! Kindly avoid code/command-only answers and briefly explain how and why your answer is applicable. [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Uzair A. Nov 23 '18 at 06:06
  • not clear what it does, this code with the question above – Nuha Dzikri Mar 03 '20 at 17:01
-1

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

Community
  • 1
  • 1
Twinscode
  • 435
  • 4
  • 7