linux调好了代码,配好了依赖,并写好了 shell 启动脚本。测试成功 。
之后通过windows 的idea 写java 调用 linux 上 shell 脚本执行程序 报错信息如下:
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/boco/querymr/task/ActiveDomainDriver : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
网上百度说是jdk版本的问题,发现无论本地 还是linux的java 环境 都是jdk1.8.
各种百度,不禁怀疑人生。后来偶然使用了sudo 命令 这时发现java 的版本突然编程了1.7
这时执行linux上的程序,爆出了相同的问题。
看来应该是 调用ssh 远程连接, 执行shell 时 的用户环境的问题。
**解决方式一:打包时降低版本
解决方式二:给程序指定jdk**
程序包中放置 jdk目录
运行jar 包时,指定jdk
export PLFMS_JDK_PATH=$BUTLERSYS_PATH/jdk1.8.0_211
$PLFMS_JDK_PATH/bin/java com.bdpp.monitor.server.start.StartHttpServer
同时附上ssh工具类
package cn.com.boco.dss.idc.ipaddressunifiedview.util;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.*;
/**
* ssh操作类
*
*/
public class RemoteExecuteCommand {
/**
* 字符编码默认是utf-8
*/
private static final String DEFAULT_CHART = "UTF-8";
/**
* 远程 IP
*/
private String ip;
/**
* 用户名
*/
private String userName;
/**
* 密码
*/
private String password;
public RemoteExecuteCommand(String ip, String userName, String password) {
this.ip = ip;
this.userName = userName;
this.password = password;
}
/**
* 获取 SSH 远程连接
*
* @return 远程连接
*/
public Connection getConnection() throws IOException {
Connection conn = new Connection(ip);
// 连接
conn.connect();
// 认证
boolean flag = conn.authenticateWithPassword(userName, password);
if(!flag) {
throw new IOException("ssh远程连接认证失败!");
}
return conn;
}
/**
* 执行指定的命令
*
* @param conn 远程连接
* @param cmd 指定的命令信息
* @return 执行的结果
*/
public String execute(Connection conn, String cmd) throws IOException {
String result;
Session session = null;
try {
// 打开一个会话
session = conn.openSession();
// 执行命令
session.execCommand(cmd);
result = processStdout(session.getStdout(), DEFAULT_CHART);
// 如果为得到标准输出为空,说明脚本执行出错了
if ("".equals(result)) {
result = processStdout(session.getStderr(), DEFAULT_CHART);
}
} finally {
if(session != null) {
session.close();
}
}
return result;
}
/**
* 将本地文件目录下所有的文件存储到远程机器指定的目录上
*
* @param conn 远程连接
* @param localPath 本地文件目录
* @param remotePath 远程文件目录
*/
public void scpFileDir(Connection conn, String localPath, String remotePath) throws IOException {
SCPClient client = new SCPClient(conn);
File file = new File(localPath);
File[] files = file.listFiles();
if(files == null) {
throw new IOException("the path no file!");
}
String[] localFiles = new String[files.length];
for (int i = 0, length = files.length; i < length; i++) {
localFiles[i] = files[i].getAbsolutePath();
}
client.put(localFiles, remotePath);
}
/**
* 将本地文件存储到远程机器指定的目录上
*
* @param conn 远程连接
* @param filePath 本地文件
* @param remotePath 远程文件目录
*/
public void scpFile(Connection conn, String filePath, String remotePath) throws IOException {
SCPClient client = new SCPClient(conn);
File file = new File(filePath);
if(file.isFile()) {
client.put(filePath, remotePath);
} else {
throw new IOException("the file not find!");
}
}
/**
* 解析脚本执行返回的结果集
* @param in 输入流对象
* @param charset 编码
* @return 流获取结果
*/
private String processStdout(InputStream in, String charset) throws IOException {
InputStream stdout = new StreamGobbler(in);
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
String line;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n");
}
return builder.toString();
}
public void close(Connection connection) {
if(connection != null) {
connection.close();
}
}
public static void main(String[] args) {
RemoteExecuteCommand command = new RemoteExecuteCommand("xxxxx", "xxx", "xxxx");
Connection connection = null;
try {
connection = command.getConnection();
String result = command.execute(connection, "sh /home/boco/mytest/start.sh");
System.out.println(result);
connection.close();
}catch (Exception e){
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}