UnsupportedClassVersionError

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

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章