Overclock.net banner
1 - 2 of 2 Posts

·
Premium Member
Joined
·
5,282 Posts
Discussion Starter · #1 ·
I have a java server that needs to run a couple of C programs to do some image processing, and I'm getting weird errors. Here is the relevant code

Code:

Code:
private String execute(String cmd){
        cmd = "/bin/bash -c \\"" + cmd + "\\"";

        output = new ArrayList<String>();

        try{    
                System.out.println(tag + "Executing [" + cmd + "]"); 
                Process p = Runtime.getRuntime().exec(cmd); 
                //.... the rest is irrelevant
        }
}

private void processFragment(byte[] bytes, float x, float y, float scale){
                 writeFile(bytes, filename);

         // Convert to pgm
         String cmd_convert = "convert " + filename + " " + pgm_filename;
         execute(cmd_convert);

         // generate keypoints
         String cmd_lkeys = "./keypoints <" + pgm_filename + " > " + lkeys_filename + " 2>/dev/null";
         execute(cmd_lkeys);

         String cmd_pkeys = "./recalckeys gpcavects.txt " + pgm_filename + " " + lkeys_filename + " " + pkeys_filename + " > /dev/null";
         execute(cmd_pkeys);

        //.... the rest is irrelevant
}
Here is the output (ignore the session prefixes and the java exception)

Code:

Code:
ession <20013fd8f4db1624> | Jpeg written (./images/20013fd8f4db1624.jpg)
Session <20013fd8f4db1624> | Executing [/bin/bash -c "convert ./images/20013fd8f4db1624.jpg ./images/image20013fd8f4db1624.pgm"]
Session <20013fd8f4db1624> | Executing [/bin/bash -c "./keypoints <./images/image20013fd8f4db1624.pgm > ./images/20013fd8f4db1624.lkeys 2>/dev/null"]
Session <20013fd8f4db1624> | ./images/20013fd8f4db1624.jpg: -c: line 0: unexpected EOF while looking for matching `"'
Session <20013fd8f4db1624> | ./images/20013fd8f4db1624.jpg: -c: line 1: syntax error: unexpected end of file
Session <20013fd8f4db1624> | <./images/image20013fd8f4db1624.pgm: -c: line 0: unexpected EOF while looking for matching `"'
Session <20013fd8f4db1624> | <./images/image20013fd8f4db1624.pgm: -c: line 1: syntax error: unexpected end of file
Session <20013fd8f4db1624> | Executing [/bin/bash -c "./recalckeys gpcavects.txt ./images/image20013fd8f4db1624.pgm ./images/20013fd8f4db1624.lkeys ./images/20013fd8f4db1624.pkeys > /dev/null"]
Session <20013fd8f4db1624> | Executing [/bin/bash -c "./match -im1 ./images/image20013fd8f4db1624.pgm -k1 ./images/20013fd8f4db1624.pkeys -im2 1.pgm -k2 1.pkeys 84 73"]
Session <20013fd8f4db1624> | gpcavects.txt: -c: line 0: unexpected EOF while looking for matching `"'
Session <20013fd8f4db1624> | gpcavects.txt: -c: line 1: syntax error: unexpected end of file
Exception in thread "Thread-0" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at #####.#######.food.server.Session.processFragment(Session.java:387)
at #####.#######.food.server.Session.processMessage(Session.java:180)
at #####.#######.food.server.Session$ListenerThread.run(Session.java:64)
Session <20013fd8f4db1624> | -im1: -c: line 0: unexpected EOF while looking for matching `"'
Session <20013fd8f4db1624> | -im1: -c: line 1: syntax error: unexpected end of file
If I copy and paste the exact command its executing it works fine

Code:

Code:
/bin/bash -c "./keypoints <./images/image20013fd8f4db1624.pgm > ./images/20013fd8f4db1624.lkeys 2>/dev/null"
Any ideas?
 

·
Registered
Joined
·
391 Posts
I don't know if your problem is the same, but some time ago I met a similar problem.
The problem was that in some (not all) linux distributions, a command like:

Quote:
Process p = Runtime.getRuntime().exec("/bin/bash -c cmd parameter parameter");
was executed passing only the 'cmd' to the bash executable, discarding the parameters.

My solution was to use the 'exec' version that take a String array as parameter, and dividing the command string in tree parts: 1) the bash command 2) the '-c' parameter 3) all the remaining parameters. The final code is like this:

Quote:
String[] command = {"/bin/sh", "-c", cmd};
Process proc = Runtime.getRuntime().exec(command);
I don't know really if your problem is the same (it's similar but...), but a test is simple.

PS: it isn't related and probably you already know the problem, but remember to create a thread to consume the stdin and stdout of the created process, otherwise the process will stop when one of the buffers become full.
 
1 - 2 of 2 Posts
Top