how to know the name of the jar that's running

I need to create a batch file that run my java jar at windows startup. To do so i need to know the name of the jar that i want to autorun. I assume that the user can change the name of the jar, so before the creation of the bat i want to check if the name of my jar is changed.

i can get the absolute position of the jar with this code, but how i can get it's name?

    File f = new File(System.getProperty("java.class.path"));
    File dir = f.getAbsoluteFile().getParentFile();
    String path = dir.toString();

thanks.

Upvotes: 4

Views: 90

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136122

One of the way is to get the URL of a class code source, eg

    URL r = JdbcTemplate.class.getProtectionDomain().getCodeSource().getLocation();
    System.out.println(r);

prints

file:/D:/.repository/org/springframework/spring-jdbc/3.2.4.RELEASE/spring-jdbc-3.2.4.RELEASE.jar

in my project

Upvotes: 3

Related Questions