繁体   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