0

Normally when i do this:

printf 'select x from Y \ngo\n' | isql -Uxx -Pxxxxxx -Dxxxxx -w 65535 -s ','

on the command line it executes fine.

even with the below java code. it works fine

public class test
{
 public static void main(String[] args)
 {
   String DB="|isql -Uxx -Pxxxxxx -Dxxxxx -w 65535 -s ','";
   String qpfx="printf \'";
   String qsfx=" \ngo\n\'";
   if(args[0]!=null)
        try {
            String cmd=qpfx+args[0]+qsfx+DB;
            System.out.println("argument query is:"+args[0]);
            System.out.println("Command is:"+cmd);

            Process p = Runtime.getRuntime().exec(new String[]{"sh","-c",cmd});

            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=reader.readLine();

            while (line != null) {    
                System.out.println(line);
                line = reader.readLine();
            }

        }
        catch(IOException e1) {}
        catch(InterruptedException e2) {}

 }
}

I will run this above java code like below:

java test "select x from Y"

Probelm comes when i give the select query as "select * from Y" instead of column names. That is when i wanted to execute like below:

java test "select * from Y"

The above jav code hangs.Can any body tell me the reason why?

below is the output where its hanging:

> java test "select * from Y"
argument query is:select * from Y
Command is:printf 'select * from Y
go
'|isql -Uxx -Pxxxxx -Dxxxxxx -w 65535 -s ','

I also tried:

java test "select \* from Y"

But it does not execute.

10
  • I dont use shell commands with Java so I may be wrong but could you try removing \ before ' in your Strings? Or are there necessary? Commented Aug 8, 2013 at 12:01
  • Does using String qsfx=" ;go;\'"; make a difference? Commented Aug 8, 2013 at 12:15
  • What does the line: System.out.println("Command is:"+cmd); print in output? Commented Aug 8, 2013 at 12:20
  • No neither removing \before ' nor putting ; worked. Commented Aug 8, 2013 at 12:22
  • So what happens when you execute printf 'select * from Y \ngo\n' | isql -Uxx -Pxxxxxx -Dxxxxx -w 65535 -s ',' from the command line? Commented Aug 8, 2013 at 12:28

1 Answer 1

0

After severe debugging i found that the line that is hanging is p.waitFor();.

I had to read this complete article and then i finally found a solution. I have shifted the p.waitFor(); after the while loop in the code and it worked.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.