Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a requirement to call Unix command from Java

The code is as follows

String strCmd = "iconv -f "+ strSrcEncoding+" -t "+ strTgtEncoding + " <<< "+"\""+InputMessage+"\"";

        String commands[] = {"bash","-c",strCmd};
        Process proc = Runtime.getRuntime().exec(commands);
        String strData = null;

        // Get the error Stream
        BufferedReader brStdError = new BufferedReader(new
                InputStreamReader(proc.getErrorStream()));
        StringBuilder sbError = new StringBuilder();


        // read any errors from the attempted command
        while ((strData = brStdError.readLine()) != null) {
            sbError.append(strData);
        }

        if(sbError.toString().isEmpty())
            return "success";
        else
            return "failure"+sbError.toString();

when i pass a large data am getting an error

"bash": java.io.IOException: error=7, Argument list too long

I tried using echo instead as below echo <> | iconv -f utf8 -t Cp930

But was getting the same error

Am i missing anything?

share|improve this question
    
You sure are missing something: mentioning that you pass the entire message to convert on the command line. –  Marko Topolnik Jun 4 '13 at 7:33
1  
Try writing to the process input stream –  uncletall Jun 4 '13 at 7:42
    
@uncletall Can u elaborate. I am not sure on how to to write to process input stream –  VamsiKrishna Jun 4 '13 at 10:02
add comment

1 Answer 1

There's a limit to much data you can pass to a program on the command line. If you have a lot of data you should pass it to iconv using its standard input stream, i.e. by writing it to proc.getOutputStream. Here's an example:

OutputStream os = proc.getOutputStream();
os.write(InputMessage.getBytes(strSrcEncoding));
os.close();

Unfortunately, for longer messages this will also fail because iconv will fill the buffer of the pipe it is connected to, and wait for the pipe to be read. The solution is writing data to iconv from one thread and reading the output from another. Dealing with external processes is a hassle because of all these pit-falls. You can read more about it here: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html Apache commons exec helps you deal with some of them.

On the other hand, why are you using iconv? You know Java has good library support for most character encodings, cp930 included?

share|improve this answer
    
I tried encoding in Java. The above code is used to check if the message can be encoded to Cp930 or not. When tested the same from java, the results were different from that when executed iconv command, so i had left with no option but to call unix commands from Java –  VamsiKrishna Jun 4 '13 at 9:47
    
Can u elaborate pass it to iconv using its standard input stream, i.e. by writing it to proc.getOutputStream –  VamsiKrishna Jun 4 '13 at 10:06
    
I've updated the answer. –  Joni Jun 4 '13 at 10:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.