shane123
shane123

Reputation: 61

Shell script working fine on linux system but not running on windows machine

I am running a java project on windows machine which reads shell script file for getting the authorization token but getting following error :

java.io.IOException: Cannot run program "./token.sh": CreateProcess error=193, %1 is not a valid Win32 application

Java program for reading the shell script:
private static String execCommand(String username){
String line;
      Process p = Runtime.getRuntime().exec("./token.sh -u " + username + " -p password123");
      BufferedReader input = new BufferedReader(new 
      InputStreamReader(p.getInputStream()));
      StringBuilder output = new StringBuilder();
      while ((line = input.readLine()) != null) {
        output.append(line);
}

How can i run the same code on windows machine.

Upvotes: 1

Views: 950

Answers (3)

Rush W.
Rush W.

Reputation: 1361

You cannot do that since the commands that the Linux bash script requires is a lot different than the windows commands. For example -

To list the contents in a directory in Linux ls

To list the contents in a directory in Windows dir

You have to write a machine/architecture/OS independent code to run across all the operating systems.

Maybe, you can try using Python scripting for that.

Or else, you can ssh from windows machine to Linux machine and run that script from windows in Linux server.

Upvotes: 1

M.P
M.P

Reputation: 11

It will not. You see the commands used in Windows CMD and Shell are different since they are completely different platforms. Even-though you use java to execute, it will not execute due to underlying fundamental difference. It is quite clear from the exception you are getting.
What can you do?

  • Read through the token.sh. Most probably the internal implementation can be implemented in Windows. Then create an if condition which checks System.getProperty("os.name") Then if its windows then call the bat file and if the OS is unix based call the sh file. For every other OS throw a valid exception.

  • Other probable way is that, if the token generated in machine independent, you can use SSH(JSch or similar) to remote connect to a UNIX server and get the token. If the token is machine dependent (if its an auth token, then probably is), try using Cygwin interpreter ,which itself does not assure you the every shell file will run in it.

  • Change the sh file and its implementation to python or ruby.Then respective interpreter may be installed on machines (which it might actually have,except for production machines).
  • Write the sh logic in Java itself rather than keeping a script file, since platform independence is actually a requirement here and you already has JRE up and running in both machines.

Upvotes: 1

Jonaswg
Jonaswg

Reputation: 404

You have to make a Windows specific implementation as well of this script. The most common and easy approach would be to use powershell.

If you want a version that works on both Windows and Unix, perhaps you should look into python.

Upvotes: 0

Related Questions