5

my problem is simple, when I try to run .sh script from Java, the script doesnt execute. If I change my script to a simple linux command, such as ls -all, it works perfectly, so I guess that I am using a bad command in my script, which stops the execution. Please help.

David

Java code:

    String cmd = "bash /home/david/burza/getter.sh";

    try {
        Process proc = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", cmd});
        BufferedReader read = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        try {
            proc.waitFor();
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
        while (read.ready()) {
            System.out.println(read.readLine());
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

Bash script:

#! /bin/bash 

wget -O data1.html http://www.rmsystem.cz/kurzy-online/akcie/easyclick;
touch ext_data.txt; 

grep 'table class="tbl1"' ./data1.html | tr '<td>' ' ' | tr '-' 'A' | grep -o -w '[0-9, ]*' | sed 's/  *//g' | sed '/^$/d' | tr ',' '.' > ext_data.txt;

lines=`wc -l ext_data.txt | grep -o '[0-9]*'`;

( echo $lines; cat ext_data.txt ) > ext_data.txt.new && mv ext_data.txt.new ext_data.txt;
3
  • are you sure your script is set as executable ? maybe chmod +x script.sh could help Commented Mar 20, 2012 at 16:52
  • What do you expect to get? Your shell script prints nothing to stdout, it redirects all output to the files. What for is BufferedReader then? Commented Mar 20, 2012 at 17:07
  • this is a stupid code, write 3 threads for input, output and error streams and do it interactively, this will save you from all sort of trouble Commented Mar 7, 2013 at 10:44

1 Answer 1

2

Start by removing the 'bash' from the start of the command.

Second, I'm not sure the shell will do #! interpretation when not used interactively. I would be minded to jump straight to /bin/bash for the Runtime.exec() call.

Finally, simply having a reader present on the stdout is not going to be enough. You need to actively be reading from it, and from stderr, to prevent the process from hanging if it writes too much output. To clarify, you're waiting for the process to complete (proc.waitFor()) before dealing with any of the output. If the process writes too much output before exiting, it will block waiting for you to empty the buffer while you're blocked waiting for it to exit.

Read all 4 pages of this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks for your advices, I managed to get it running using the javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1 thanks a lot.
The JavaWorld link is broken can any keywords I can search for this link in the JavaWorld and fix it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.