I want to execute an exe file (say, a.out) in hp-ux 10.20 from the java program.
1) The java code Runtime.getRuntime().exec(". ./a.out"); is NOT WORKING
2) The java code Runtime.getRuntime().exec("a.out"); is NOT WORKING
3) I wrote a script (say, test) #!/bin/sh a.out Then i called this script from the java program Runtime.getRuntime().exec("test"); This is also NOT WORKING 4) I wrote a script (say, test) a.out Then i called this script from the java program Runtime.getRuntime().exec(new String[] {"/bin/sh", "test"}); This code is WORKING Could someone please explain me why the codes (1, 2 and 3) are not working and why the code 4 is working...Thanks a lot...
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
My guess is that it is one of two problems:
you may have assumed the wrong "current directory". Notice that the only one which works is the one with a full path to "/bin/sh". Can you try one of the other examples but using the full path to the executable, ie "/home/someone/tests/a.out" or whatever ?
you may have forgotten to make your examples executable. Remember that for any *nix system to execute a binary or script file it needs to have the "x" bit set for an appropriate user. Try ls -l a.out and look for something like rwxr-xr-x . If there are no 'x' characters, try chmod 755 a.out then try to execute it again.
Hi Frank Carver: Thank you very much for your reply. I tried with full path...it is working fine. Once again, thanks. But i have a doubt now 1) Runtime.exec("java MyExample") works fine...where MyExample is a .class file in the directory /home/someone/test. It is WORKING FINE WITHOUT GIVING FULL PATH 2) Runtime.exec("a.out") is not working...where a.out is an exe file in the directory /home/someone/test. It is NOT WORKING WITHOUT GIVING FULL PATH 3) Runtime.exec("/home/someone/test/a.out") is working...where a.out is an exe file in the directory /home/someone/test. It is WORKING BY GIVING FULL PATH Could you please explain me what is happening in all the three cases. Also please explain the code 3 and 4 what i had written yesterday... Waiting for your reply to understand the concepts behind it. Thanks a lot. Uma
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
This situation arises because your current directory is not in your PATH, whereas the directory containing the "java" executable is in the PATH. type echo $PATH to see what is in your path. On most modern *nix systems, the current directory (.) is not in the PATH, as it is seen to present too much of a security risk. You might be able to get this working without a full path by using . as the path for the command ie.: Runtime.getRuntime().exec("./a.out"); This is almost like your original example 1, but doesn't have the leading "." and space, which only work in the context of a shell.
Uma Viswanathan
Ranch Hand
Joined: Jun 14, 2001
Posts: 126
posted
0
Hi Frank Carver: Please do confirm the following: 1) I had written "The code Runtime.getRuntime().exec("a.out") is not working...where a.out is an exe file in the directory /home/someone/test. It is NOT WORKING WITHOUT GIVING FULL PATH" The reason is: Runtime.getRuntime().exec tries to search the a.out in the home directory...i tested by copying the a.out to the home directory and execute the above code. It works fine.
2) I had written "I wrote a script (say, test) #!/bin/sh a.out Then i called this script from the java program Runtime.getRuntime().exec("test"); This is also NOT WORKING" The reason is: Runtime.getRuntime().exec does not spawn a shell but we need a shell to run the script. The test script above (which contains the command #!/bin/sh) does not spawn a shell...so, it is not working. 3) I had written "I wrote a script (say, test) a.out Then i called this script from the java program Runtime.getRuntime().exec(new String[] {"/bin/sh", "test"}); This code is WORKING" The reason is: This works bcoz the first parameter to exec (i.e., /bin/sh) spawns a shell. Frank Carver: please confirm whether my understanding is correct or not....Also add your views/points... 4) I had written "Runtime.exec("/home/someone/test/a.out") is working...where a.out is an exe file in the directory /home/someone/test. It is WORKING BY GIVING FULL PATH" Here we are hard coding the directory name, which will not be good in a project. So, could you please tell me the way to specify the directory name. Thanks a lot. Uma
Uma Viswanathan
Ranch Hand
Joined: Jun 14, 2001
Posts: 126
posted
0
Sorry..i did not see ur previous reply when i had posted my earlier one...i typed the message and then it was time to go for the meeting...after i came back...i just clicked "Add reply"...
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
No problem. I'm sure we all do that from time to time. Please let me know if my other suggestion does work, though.
Uma Viswanathan
Ranch Hand
Joined: Jun 14, 2001
Posts: 126
posted
0
Hi Frank Carver: Thanks for your reply. Runtime.getRuntime().exec("./a.out"); is not working but it is working if i include . (i.e, current directory) in the PATH. Also i am able to run the exe thru a script. Thanks a lot for your ideas. Uma