Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOF while looking for matching `"' error

sh -c "cd /home/dipankar/NetBeansProjects/TransBench/Hindi;./mat"

When executing following command in linux terminal its executes perfectly. However, when I am trying to run the same using java runtime its gives the following error :

ERROR>/home/dipankar/NetBeansProjects/TransBench/Hindi;./mat": -c: line 0: unexpected EOF while looking for matching `"'
ERROR>/home/dipankar/NetBeansProjects/TransBench/Hindi;./mat": -c: line 1: syntax error: unexpected end of file

Please help I am new to linux.

Runtime rt = Runtime.getRuntime();               
Process proc = rt.exec("sh -c \"cd /home/dipankar/NetBeansProjects/TransBench/Hindi;./mat\"");
proc.waitFor();
like image 813
CrazyLogic Avatar asked Sep 18 '25 01:09

CrazyLogic


1 Answers

Given the tag and the symptoms, I expect that you are using an exec method that takes the command as a single String.

That won't work. The problem is that the method uses a very simple scheme to "parse" the command string into a command name and arguments. It simply splits the string where there is whitespace ... ignoring any quoting, and any other shell stuff.

What you need to do is to use the overload that takes a String[]; e.g.

    ....exec(new String[]{
            "sh", 
            "-c", 
            "cd /home/dipankar/NetBeansProjects/TransBench/Hindi;./mat"
    });
like image 77
Stephen C Avatar answered Sep 20 '25 14:09

Stephen C