How to tell if JAR file is ran by a sh script or a bat script in JAVA

Hi I have a JAR file which can be ran by both a bat file and an sh file. Within the JAR I want some sort of variable flag that tracks whether the JAR has been run by a bat file or an sh file. Looked around stackoverflow but couldn’t find a similar topic. Is that possible? Thanks.

This should not be an issue. In you bat or sh set up an environment variable (just before your jar is started) that is quite specific so that it is unlikely that any other servise would set up the veriable of the same name (or think out some specifi value of the variable). In your code read the variable so that your code can detect where it was started from.

Check-out the details here.

You’ll need to get the parent process of java and check it.The easiest way is with powershell (it is installed by default from windows 7/server 2008):

public static void main(String[] args){
    if (System.getProperty("").toLowerCase().contains("windows")){
        String pid=ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
        String pscommand="powershell $pp=(gwmi win32_process|Where-Object {$_.ProcessId -eq "+pid+"}).ParentProcessId;(gwmi win32_process |Where-Object {$_.ProcessId -eq $pp}).Caption";
        Process powerShellProcess;
        int exitCode=0;
        String shell="";

        try {
            powerShellProcess = Runtime.getRuntime().exec(pscommand);

            BufferedReader stdInput = new BufferedReader(new

            //BufferedReader stdError = new BufferedReader(new
            //        InputStreamReader(powerShellProcess.getErrorStream()));

            String s = stdInput.readLine();
                System.out.println("called from batch or cmd.exe directly");
            } else if (s.toLowerCase().equals("bash.exe")){
                System.out.println("called from bash.exe or .sh script");
            } else {
                System.out.println("called from smething else");

            System.out.println("Exit code from getting shell type: "+exitCode );
        } catch (Exception e) {