2

I face a problem to run a ".sh" file from my Java code, which is written in bash. At this point, I am using the following command to run the script (which is in the local directory, and seems to be found):

Process proc1 = Runtime.getRuntime().exec("./"+ nomScript);
proc1.waitFor();

But when executing, I get the error:

CreateProcess error=193, %1 n’est pas une application Win32 valide

(%1 is not a valid Win32 application, the whole error is in the code section below).

java.io.IOException: Cannot run program "./OAD_TCI_20_Blocage.sh": CreateProcess error=193, %1 n’est pas une application Win32 valide
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at com.coframi.oad.manager.ExecSuperviseur.gererBlocage(ExecSuperviseur.java:406)
at com.coframi.oad.manager.ExecSuperviseur.main(ExecSuperviseur.java:173)
Caused by: java.io.IOException: CreateProcess error=193, %1 n’est pas une application Win32 valide
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 5 more

I tried few things to run the script, as pointing the path to bash.exe as:

Process proc1 = Runtime.getRuntime().exec("pathTobash.exe -c "+ nomScript);

But nothing seems to work.

Does anyone know how I could run this script? Thank you

neau
  • 48
  • 8
MatthChtp
  • 21
  • 3
  • 2
    Well, are you trying to execute a ".sh" script in windows. Windows tell you that this is not a valid script, most likely because this is a script for a Unix. Can you execute the script without using Java ? – AxelH Apr 24 '19 at 08:09
  • @AxelH No, I necessarily have to execute the script from java, because of the running of the application I work on. I found out that only files like '.exe', '.bat'... are considered as valid applications, but can't find any way to run directly the '.sh' – MatthChtp Apr 24 '19 at 08:17
  • 3
    You don't get my point, if you can't execute that script on your OS, Java will not be able to help you because `Runtime.exec` will not translate anything, it will just run the process using the OS terminal. – AxelH Apr 24 '19 at 08:20
  • @AxelH Sorry, I didn't understood in this way. The script is correctly running on my OS when executed without using java – MatthChtp Apr 24 '19 at 08:23
  • Just to be sure, dont you are trying to run `java -> exe -> .sh` ? Right? If yes, then its the same as trying to run `sh` directly / from java, but only you will get the error not directly from shell, but from executed exe via java, basically same. – xxxvodnikxxx Apr 24 '19 at 08:25
  • 1
    Could you try `"pathTobash.exe " + nomScript` (that is, without `-c`)? – MC Emperor Apr 24 '19 at 08:26
  • 1
    See linked answer for some working examples; basically you need to pass a String array with command and args, not a single string. – Gyro Gearless Apr 24 '19 at 08:29
  • it is a Unix script file. So, it's normal if you can't run it on windows. That's why it is very unencouraged to use the script for special for some platforms on his project if you think to use it on any platform – JessGabriel Apr 24 '19 at 09:24
  • checkout jsch example mentioned here :- https://stackoverflow.com/questions/2405885/run-a-command-over-ssh-with-jsch/11902536#11902536 – N29 Apr 24 '19 at 09:41
  • Finally, I just had to replace "./" with "sh ", as Runtime.exec() calls a Windows command and not Unix. Thank for help! – MatthChtp Apr 24 '19 at 12:32
  • @MatthChtp, can you explan how did it help? What you replaced with what, may be detailed way? What is nomScript in the original question? – tester Feb 05 '20 at 16:14

0 Answers0