簡體   English   中英

從Web存檔中的Java代碼通過Runtime.exec運行命令(在Jetty中部署了WAR)

[英]Run a command via Runtime.exec from java code inside Web Archive (WAR deployed in Jetty)

我的PC在Windows7 64位下運行,我有一個實用程序(.exe,非常舊的[〜WinXP時代],沒有可用的源),可以從部署到Jetty的Java代碼中調用該實用程序。 如果從控制台啟動實用程序,則不會出現任何錯誤。 如果我通過簡單的Java包裝器啟動實用程序:

import java.util.*;
import java.io.*;
public class Wrapper {
    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("java.version"));
        Runtime.getRuntime().exec("util.exe -opt1 -opt2");
    }
}

我也沒有錯誤。 但是如果我從WAR內調用此代碼(比Runtime.getRuntime()。exec(“ util.exe”復雜一點,因為我需要計算二進制文件的絕對路徑),則我得到IOException並顯示以下消息:

CreateProcess錯誤= 216,此版本的%1與您正在運行的Windows版本不兼容。 檢查計算機的系統信息以查看是否需要該程序的x86(32位)或x64(64位)版本,然后與軟件發行商聯系。

我嘗試使用-d32選項啟動jetty,嘗試將不同版本的Java(均為JRE / JDK,均為6/7,均為32/64位)放入JAVA_HOME和PATH中,但沒有成功。

有沒有人遇到類似的問題? 有可能解決它們嗎?

[更新]我已經附加了一些服務器代碼。 CommandLine和FileUtils是Apache Commons的一部分。 ApplicationContextSpring框架有關

public class ImageLoader implements ApplicationContextAware {
    private final static String UTIL_EXECUTABLE = "util.exe";
    private final static String TEMP_FILE_PREFIX = "tmpFilePrefix";

    private ApplicationContext applicationContext;
    private File binaryPath;

    @PostConstruct
    public void init() throws Exception {
        if (applicationContext instanceof WebApplicationContext) {
            Resource binaryRoot = applicationContext.getResource(
                    "WEB-INF/classes/executable");
            this.binaryPath = binaryRoot.getFile();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * Download db image from device.
     */
    public byte[] downloadImage(Device device) throws LoaderException {
        try {
            File file = downloadWindows(device);
            return FileUtils.readFileToByteArray(file);
        } catch (Exception ex) {
            throw new LoaderException("Error downloading file: " + ex.getMessage(), ex);
        }
    }

    private File downloadWindows(final Device device) throws Exception {
        File tmpFile = File.createTempFile(TEMP_FILE_PREFIX, null);
        CommandLine command = generateCommand(ActionType.download, tmpFile, device.getTargetIP(), "user", "pass");
        Runtime.getRuntime().exec(command.toString());
        return tmpFile;
    }

    protected CommandLine generateCommand(ActionType actionType, File file, String targetIP, String userName, String userPassword) throws IOException {
        String bin = this.binaryPath.getPath() + "\\" + UTIL_EXECUTABLE; 
        // safe to use \\ because code only runs if WAR deployed under Windows    
        CommandLine commandLine = new CommandLine(bin.replace("\\", "\\\\"));
        commandLine.addArgument(actionType.name());
        commandLine.addArgument(file.getAbsolutePath().replace("\\", "\\\\"));
        commandLine.addArgument(targetIP);
        commandLine.addArgument(userName);
        commandLine.addArgument(userPassword);
        return commandLine;
    }
}

enum ActionType {
    download,
    upload
}

可恥的是我和我的注意力不集中。 問題實際上出在“其他地方”。 WAR由maven構建,並且可執行文件的處理方式與其他任何資源一樣,因此校驗和與原始文件相比有所不同。 我從filteing中排除了exe文件(在pom.xml中),它開始正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM