Get name of running Jar or Exe

前端 未结 5 2121
长发绾君心
长发绾君心 2020-12-03 15:47

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 h

5条回答
  •  时光说笑
    2020-12-03 16:27

      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");
    

提交回复
热议问题