문제

Is there any way, from within a Java program compiled by gcj, to find out the name of the executable the user ran to start the program?

In C, argv[0] (from inside main) is the name, but in Java, the args array given to main contains only the arguments to the main class. When running with a normal java command line, that makes some sense because the executable would always be java (or java.exe).

When running a Java program which has been compiled by gcj, however, the name of the executable which the user ran might be useful information. Ideally I'd like to find that name without resorting to platform specific approaches like reading /proc (and whatever the equivalent would be for Windows), but perhaps that isn't possible.

도움이 되었습니까?

해결책

After much searching on Google Code Search, I've discovered that the program name is stashed in the gnu.gcj.progname property. Sample program:

public class ProgName {
    public static void main(String... args) {
        System.out.println(System.getProperty("gnu.gcj.progname"));
    }
}

Edited to add: gnu.gcj.progname is documented in libgcj Runtime Properties too, so luckily this isn't just an internal implementation detail.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top