0

When I execute the below command on command line, it shows all the stored procedures and tables in the sybase DB.

printf 'sp_help\ngo\n' | isql -Uxx -Pxxxx -Dxxxxx

But when I do the same thing in java. This does not return any result. Can anyone tell me what is the problem with my code below:

public class test
{
  public static void main(String[] args)
  {
   String cmd = "printf "+"\'sp_help\ngo\n\'"+"| isql -Uxx -Pxxxx -Dxxxxx" ;
   try{ 
          Process p;
          p = Runtime.getRuntime().exec(cmd);
          p.waitFor();
          String line; 
          BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

           while ((line = reader.readLine())!=null){
           System.out.println("Row is :" + line);
   } catch(Exception e)
       {
           System.out.println("Exception Caught : " + e);
       }

  }

}

EDIT I executed it as below suggested by Darkdust but still it doesnt work.

try{
          Process p;
          p = Runtime.getRuntime().exec("sh -c \'printf \"sp_help\ngo\n\" | isql -Uxx -Pxxxxx -Dxxxxxx\'");
          p.waitFor();
          String line;
          BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

           while ((line = reader.readLine())!=null){
           System.out.println("Row is :" + line);
  }
 }
       catch(Exception e)
       {
           System.out.println("Exception Caught : " + e);
       }

But the command :

sh -c 'printf "sp_help\ngo\n" | isql -Usa -Psybase11 -Dcnadb'

Works on command line.

I also tried with :

p = Runtime.getRuntime().exec(new String[]{"sh","-c","\'printf \"sp_help\ngo\n\"","|isql -Uxx -Pxxxxx -Dxxxxx\'"});

But with no sucess.

1 Answer 1

1

Several things come to mind:

  • Incomplete PATH environment variable (thus isql can't be found).
  • If it's a command you provide, instead of messing with PATH you might want to make sure your are you in the correct working directory and call ./isql instead.
  • Since you're using a pipe, you should let a shell execute this as in sh -c "foo | bar". Otherwise the | isql ... part is passed as argument to printf as well.
Sign up to request clarification or add additional context in comments.

6 Comments

isql PATH is available. As i said that the command runs as expected from command line. Why the same is not running as expected in java? And i think the whole cmd will be executed on command line and output will be retrieved into java
See my last point. Java might run "printf" and pass everything as arguments to "printf", even the pipe symbol and everything afterwards. You need to run a shell and let that run your command, as in sh -c "printf 'sp_help\ngo\n' | isql -Uxx -Pxxxx -Dxxxxx". You probably want to use the exec variant with an String[] argument: IIRC, it should be Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd}); (my Java is rusty).
OK.. I got your point. But the problem now i am facing is sh -c "printf "sp_help\ngo\n" | isql -Uxx -Pxxx -Dxxxxx" is not working on the command line itself if i add sh -c as prefix to the command.It gives me an error Usage: printf format [argument...]
No, I do not mean to write a bash script. On the shell, you would need to enter sh -c 'printf "sp_help\ngo\n" | isql -Uxx -Pxxx -Dxxxxx' (or use " and escape them inside with \"). Most likely all you need to do is replace your p = ... line with p = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd}); (in this case no extra " escaping is necessary). Have you tried that?
Did you happen to fix the issue?
|

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.