This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes IOException while executing runtime.exec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "IOException while executing runtime.exec" Watch "IOException while executing runtime.exec" New topic
Author

IOException while executing runtime.exec

Himanshu Gupt
Greenhorn

Joined: Dec 03, 2006
Posts: 13
I am trying to execute one shell script from another folder using below mentioned code



it is throwing IOException, ca you please tell me if I am doing something wrong.

Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
Are you handling the input and output streams of the process you started correctly? See this article for how to do it.


Ping & DNS - my free Android networking tools app
Himanshu Gupt
Greenhorn

Joined: Dec 03, 2006
Posts: 13
please find my code attached below




I am getting following exception

java.io.IOException: No such file or directory
at 0x0000002a95b6c946: java.lang.Throwable.Throwable(java.lang.String) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b60039: java.lang.Exception.Exception(java.lang.String) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b73379: java.io.IOException.IOException(java.lang.String) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b4f67b: java.lang.ConcreteProcess.startProcess(java.lang.String[], java.lang.String[]) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c78c7e: java.lang.ConcreteProcess.ConcreteProcess(java.lang.String[], java.lang.String[]) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b6308e: java.lang.Runtime.exec(java.lang.String[], java.lang.String[]) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b62fee: java.lang.Runtime.exec(java.lang.String, java.lang.String[]) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b62f4c: java.lang.Runtime.exec(java.lang.String) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c92d74: ffi_call_UNIX64 (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c92ba4: ffi_call (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c9223f: ffi_java_raw_call (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b39755: _Jv_InterpMethod.continue1(_Jv_InterpMethodInvocation) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b39ede: _Jv_InterpMethod.run(ffi_cif, void, ffi_raw, _Jv_InterpMethodInvocation) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b37d3a: _Jv_InterpMethod.run_normal(ffi_cif, void, ffi_raw, void) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c922b6: ?? (??:0)
at 0x0000002a95c92ca9: ffi_closure_UNIX64_inner (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c92e5c: ?? (??:0)
at 0x0000002a95c92d74: ffi_call_UNIX64 (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c92ba4: ffi_call (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c9223f: ffi_java_raw_call (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b39755: _Jv_InterpMethod.continue1(_Jv_InterpMethodInvocation) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b39ede: _Jv_InterpMethod.run(ffi_cif, void, ffi_raw, _Jv_InterpMethodInvocation) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b37d3a: _Jv_InterpMethod.run_normal(ffi_cif, void, ffi_raw, void) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c922b6: ?? (??:0)
at 0x0000002a95c92ca9: ffi_closure_UNIX64_inner (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95c92e5c: ?? (??:0)
at 0x0000002a95b49e75: gnu.gcj.runtime.FirstThread.call_main() (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95bb06a8: gnu.gcj.runtime.FirstThread.run() (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b55225: _Jv_ThreadRun(java.lang.Thread) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000002a95b278b8: _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/../lib64/libgcj.so.3)
at 0x0000000000400be2: ?? (??:0)
at 0x0000002a9648f197: __libc_start_main (/lib64/tls/libc.so.6)
at 0x00000000004009aa: ?? (??:0)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
It looks like you aren't handling the I/O streams at all. The article also tells you why the Runtime.exec(String) method isn't the best choice to use when compared to the other ones.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes - although the error you're getting doesn't sound like it's related to either. Processing those output streams in separate threads as shown in the article is always a good thing to do. But here are some additional things to try:
  • Verify that /home/abc is correct and that the command runs correctly from the command line.
  • Replace "test.sh" with "./test.sh". The spawned process may not have the current directory in the PATH.
  • Use ProcessBuilder (JDK 5+) instead. Set the directory using the directory() method.
  • Replace "test.sh" with ". test.sh" or "/usr/bin/bash test.sh".

  • For all these, you will probably get more useful diagnostic info if you process those output streams to print any error messages on the screen.
    [ January 25, 2008: Message edited by: Jim Yingst ]

    "I'm not back." - Bill Harding, Twister
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19723
        
      20

    cd /home/abc/test123 ; test.sh

    That's not one single process - those are two processes separated by a ;.
    No usually your shell will handle it for you, but just like using piping (|) and input/output redirecting (< and > , you can't use it in Java processes.

    Now possibly you could just use "/home/abc/test123/test.sh", but that will fail if the script has any relative references to files in /home/abc/test123. Alternatively, you can create a new script that performs these two actions.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: IOException while executing runtime.exec