0

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.

olliejjc16
  • 361
  • 5
  • 20
  • I suppose you want to check this on windows machine? It is not possible to run `bat` on unix (though on unix you have others shells besides bash) – npocmaka Jul 27 '17 at 12:41
  • 1
    Ya it shouldn't matter what machine they're using only the type of script they are running. Bat and sh for Windows and just sh for Linux. – olliejjc16 Jul 27 '17 at 12:43
  • 1
    It depends on the platform. Windows supports bat files; UNIX and friends support sh files. And what difference does it make to your application how it was started? – user207421 Jul 27 '17 at 12:49
  • @EJP - you can start `.sh` file on windows if you have microsoft's bash installed or with cygwin. – npocmaka Jul 27 '17 at 13:26
  • Your Java code shouldn't care how it is started. – chepner Jul 27 '17 at 16:44

2 Answers2

0

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.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
0

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("os.name").toLowerCase().contains("windows")){
        String pid=ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
        System.out.println(pid);
        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
                    InputStreamReader(powerShellProcess.getInputStream()));

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

            String s = stdInput.readLine();
            System.out.println(s);
            if(s.toLowerCase().equals("cmd.exe")){
                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");
            }

            powerShellProcess.getOutputStream().close();
            powerShellProcess.getInputStream().close();
            powerShellProcess.getErrorStream().close();
            exitCode=powerShellProcess.waitFor();
            System.out.println("Exit code from getting shell type: "+exitCode );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
npocmaka
  • 55,367
  • 18
  • 148
  • 187