【发布时间】:2016-08-17 00:28:48
【问题描述】:
我正在尝试执行一个简单的 linux 命令,通过我的 java 代码将一些文本附加到远程服务器中的文件中。但它不起作用。当我在 linux 框中运行相同的命令时,它工作正常。
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session sessionwrite = jsch.getSession(user2, host2, 22);
sessionwrite.setPassword(password2);
sessionwrite.setConfig(config);
sessionwrite.connect();
System.out.println("Connected");
Channel channel = sessionwrite.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(
channel.getInputStream()));
String command = "echo \"hello\" >> welcome.txt";
((ChannelExec) channel).setCommand(command);
System.out.println("done");
}
【问题讨论】:
-
不工作是什么意思?你认为
welcome.txt最终会在哪里?您是否尝试过使用shell频道?