Runtime.exec() command with utf-8 characters

孤者浪人 提交于 2021-02-07 04:38:11

问题


I'm trying to execute Runtime.getRuntime().exec() command which contains polish characters. Command is cutted from the first polish letter. Instead of polish letter i'm getting "?".

How can I set correct encoding to exec this command properly?

Command which i'm using : execCommand("php /home/script/url param1 param2 param3)

execCommand looks like :

private String execCommand(String command)
{
    String output="";
    try
    {
        Process proc = Runtime.getRuntime().exec(command);
        BufferedReader read = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        try
        {
        proc.waitFor();
        }
        catch(InterruptedException e) 
        {
            output=e.getMessage();
        }
        while(read.ready())
        {
            output=read.readLine();
        }
    }
    catch(IOException e)
    {
        output=e.getMessage();
    }

    return output;
}

回答1:


You could try the following:

String command = URLEncoder.encode("<your command>", "utf-8");

You could then try to run that via Runtime.exec

You could also check if your JDK actually supported:

Charset.forName("UTF-8")

If this fails for any reasons then there's probably something wrong/not configured in your JDK



来源:https://stackoverflow.com/questions/15394986/runtime-exec-command-with-utf-8-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!