Call Shell Script From java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaskarbasak
    New Member
    • Feb 2008
    • 27

    Call Shell Script From java

    Hi All,

    i want to run shell script from java.shell script run successfully and generate a file.
    But when i want to check the existence of that file,it shows that the file is not present.

    please help me to check the file from java or how could i know the proccess(Shell script) is finished.

    i have given my code below.

    package com;

    import java.io.*;
    import java.util.*;


    public class ScriptBuilder {
    public ScriptBuilder() {
    }

    public void writeScript() throws java.io.IOExcep tion {
    Runtime rt = Runtime.getRunt ime();
    rt.exec("chmod 777 testinglinux.sh ");
    Process p = rt.exec("./testinglinux.sh ");
    try {
    p.waitFor();
    } catch (Exception e) {
    e.printStackTra ce();
    }
    String shellfilename = "testing.tx t";
    if (new File(shellfilen ame).exists()) {
    System.out.prin tln(shellfilena me+" File exists");
    }
    }

    public static void main(String[] args) throws java.io.IOExcep tion {
    ScriptBuilder sb = new ScriptBuilder() ;
    sb.writeScript( );
    }
    }

    Thanks!
    Vaskar
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vaskarbasak
    Hi All,

    i want to run shell script from java.shell script run successfully and generate a file.
    But when i want to check the existence of that file,it shows that the file is not present.

    please help me to check the file from java or how could i know the proccess(Shell script) is finished.

    i have given my code below.

    package com;

    import java.io.*;
    import java.util.*;


    public class ScriptBuilder {
    public ScriptBuilder() {
    }

    public void writeScript() throws java.io.IOExcep tion {
    Runtime rt = Runtime.getRunt ime();
    rt.exec("chmod 777 testinglinux.sh ");
    Process p = rt.exec("./testinglinux.sh ");
    try {
    p.waitFor();
    } catch (Exception e) {
    e.printStackTra ce();
    }
    String shellfilename = "testing.tx t";
    if (new File(shellfilen ame).exists()) {
    System.out.prin tln(shellfilena me+" File exists");
    }
    }

    public static void main(String[] args) throws java.io.IOExcep tion {
    ScriptBuilder sb = new ScriptBuilder() ;
    sb.writeScript( );
    }
    }

    Thanks!
    Vaskar
    There is a File.exists() method. Will that do for you?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Having looked over your question again, I realize now that my response is meaningless.

      What you need is this article which explains the traps one runs into when using Runtime.exec.

      Comment

      • satch
        New Member
        • Feb 2008
        • 23

        #4
        Originally posted by vaskarbasak
        Hi All,

        i want to run shell script from java.shell script run successfully and generate a file.
        But when i want to check the existence of that file,it shows that the file is not present.

        please help me to check the file from java or how could i know the proccess(Shell script) is finished.

        i have given my code below.
        [CODE=java]
        package com;

        import java.io.*;
        import java.util.*;


        public class ScriptBuilder {
        public ScriptBuilder() {
        }

        public void writeScript() throws java.io.IOExcep tion {
        Runtime rt = Runtime.getRunt ime();
        rt.exec("chmod 777 testinglinux.sh ");
        Process p = rt.exec("./testinglinux.sh ");
        try {
        p.waitFor();
        } catch (Exception e) {
        e.printStackTra ce();
        }
        String shellfilename = "testing.tx t";
        if (new File(shellfilen ame).exists()) {
        System.out.prin tln(shellfilena me+" File exists");
        }
        }

        public static void main(String[] args) throws java.io.IOExcep tion {
        ScriptBuilder sb = new ScriptBuilder() ;
        sb.writeScript( );
        }
        }
        [/CODE]
        Thanks!
        Vaskar
        As I am in windows environment, I tried your program by changing the shell scrip to a batch file and it worked fine. I dont know any reason why it should fail for you.
        To check the return status of the process use the exitValue method. A successful completion would return 0 exit value.

        Comment

        Working...