How to get the name of a Java program?

Make 'name of program' a property that is passed to your program via '-D' command-line switch, like so

java -Dprogram.name=myApp.jar -jar myApp.jar

Read it in your code like so

if ("myApp.jar".equals(System.getProperty("program.name"))) {
   // perform appropriate actions...
}

The actual program running the JAR file would be java.exe.

I suggest you approach the problem from a completely different angle and have the exe wrapper set a system property that the program queries. Or you could have it and the JAR manifest specify different main classes.

Tags:

Java