使用JSch执行iqisql失败,使用"尝试分配与本地化相关的结构时发生错误"

时间:2016-09-26 20:39:30

标签: java sybase jsch remotecommand

我需要在远程计算机上运行脚本,因为我正在使用Jsch 我无法将脚本存储在远程计算机中。 下面是脚本

#!/bin/bash
/eniq/sybase_iq/OCS-15_0/bin/iqisql -Uce -Pce -Sendb -b -n <<EOF!

SELECT SUM(COUNTERS) FROM ADAPTER WHERE STARTTIME = "Aug  8 2016 11:30:00.000000PM"

go
exit
EOF!

如果我在Linux机器上运行脚本,我就可以获得所需的输出

#: /eniq/sybase_iq/OCS-15_0/bin/iqisql -Uce -Pce -Sendb -b -n <<EOF!
> SELECT SUM(COUNTERS) FROM ADAPTER WHERE STARTTIME = "Aug  8 2016 11:30:00.000000PM"
> go
> exit
> EOF!
                                   168002

 (1 row affected)

但是当我尝试使用以下java代码运行脚本时 -

package org.rhq.plugin.eniq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 import com.jcraft.jsch.Channel;
 import com.jcraft.jsch.ChannelExec;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.Session;

 public class JSchTest {

    private final Log log = LogFactory.getLog(JSchTest.class);

    public void executeCommand() {
        try {
            String command = "/eniq/sybase_iq/OCS-15_0/bin/iqisql -Uce -Pce -Sendb -b -n <<EOF!" + "\n"
                    + "SELECT SUM(COUNTERS) FROM ADAPTER WHERE STARTTIME = \"Aug  8 2016 11:30:00.000000PM\""
                    + "\n" +

                    "go" + "\n" + "exit" + "\n" + "EOF!";
            String host = "107.250.237.13";
            String user = "dcuser";
            String password = "dcuser";

            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            ;
            session.setPassword(password);
            session.connect();

            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            InputStream input = channel.getInputStream();
            channel.connect();

            log.error("Channel Connected to machine " + host + " server with command: " + command);

            try {
                InputStreamReader inputReader = new InputStreamReader(input);
                BufferedReader bufferedReader = new BufferedReader(inputReader);
                String line = null;

                while ((line = bufferedReader.readLine()) != null) {
                    log.error("Eniq DB Output line is " + line);
                }
                bufferedReader.close();
                inputReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            channel.disconnect();
            session.disconnect();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

但是当我运行这个时,我得到以下错误作为outut-尝试分配与本地化相关的结构时发生错误

与区域设置名称有关&#34; en_US.UTF-8&#34; ?如何在jsch中设置

N.B:由于要求不允许,我不能连接使用sybase驱动程序

1 个答案:

答案 0 :(得分:0)

根据之前的StackOverflow回答

not able to run isql on cygwin

你应该做

unset LANG

在运行isql之前。所以在运行iqisql之前可能也是如此。