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.

5
  • 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) Commented Jul 27, 2017 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. Commented Jul 27, 2017 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? Commented Jul 27, 2017 at 12:49
  • @EJP - you can start .sh file on windows if you have microsoft's bash installed or with cygwin. Commented Jul 27, 2017 at 13:26
  • Your Java code shouldn't care how it is started. Commented Jul 27, 2017 at 16:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.