无法使用execl()启动jar

时间:2015-09-30 08:07:21

标签: java c++ unix jar execl

我收到了decrypt.jarencrypt.jar文件,用于在传输之前准备文件。

当我启动终端并输入:

/usr/bin/java -jar /path/to/jar/decrypt.jar

我得到了输出:

No input file specified

哪个好!罐子工作。现在在我的代码中,当我使用execl()启动jar时,我将其作为输出:

Error: Could not find or load main class util.decrypt.jar
Decryptor exited with 0

请注意,这里的问题是java试图启动实际上是jar路径的类(路径是util / decrypt.jar,并将其作为类util.decrypt.jar执行)

我的代码:

bool decrypt_file(const std::string& file) {
    int result;
    int pipefd[2];
    FILE *cmd_output;
    char buf[1024];
    int status;

    result = pipe(pipefd);
    if (result < 0) {
        throw "pipe error!";
    }

    pid_t pid = fork(); /* Create a child process */
    const std::string decryptJar = "util/decrypt.jar";
    int ex;
    if ( !fileExists(decryptJar) ) throw "File decryptor does not exist!";

    switch (pid) {
        case -1: /* Error */
#ifdef _DEBUG
            std::cout<<"fork() failed!\n";
#endif
            return false;
        case 0: /* Child process */
            dup2(pipefd[1], STDOUT_FILENO); /* Duplicate writing end to stdout */
            close(pipefd[0]);
            close(pipefd[1]);

            //getJava() returns "/usr/bin/java"
            ex = execl(Config::getInstance().getJava().c_str(), "-jar", decryptJar.c_str(), file.c_str(), NULL); /* Execute the program */
#ifdef _DEBUG
            std::cout << "execl() failed! returned "<<ex<<", errno = "<<errno<<"\n"; /* execl doesn't return unless there's an error */
            //todo if errno is 2, java was not found on the system, let the user know!
#endif
            return false;
        default: /* Parent process */
            int status;
            close(pipefd[1]); /* Close writing end of pipe */
            cmd_output = fdopen(pipefd[0], "r");
#ifdef _DEBUG
            if (fgets(buf, sizeof buf, cmd_output)) {
                std::string str(buf);
                std::cout<<"OUTPUT: "<<str<<"\n";
            }
#endif
            while (!WIFEXITED(status)) {
                waitpid(pid, &status, 0); /* Wait for the process to complete */
            }

#ifdef _DEBUG
            std::cout << "Decryptor exited with " << WEXITSTATUS(status) << "\n";
#endif
            return true;
    }
}

jar中的清单是正确的(它是由eclipse生成的):

Manifest-Version: 1.0
Class-Path: .
Main-Class: com.{...}.Decryptor

添加:

尝试将路径更改为jar的绝对路径并没有解决问题。

const std::string decryptJar = workingDir() + "/util/decrypt.jar";

1 个答案:

答案 0 :(得分:1)

<强>解决

我身边的愚蠢错误,按照惯例,第一个参数应该始终是可执行文件的路径。

所以

ex = execl(Config::getInstance().getJava().c_str(), "-jar", decryptJar.c_str(), file.c_str(), NULL); /* Execute the program */

应该是

ex = execl(Config::getInstance().getJava().c_str(), decryptJar.c_str(), "-jar", decryptJar.c_str(), file.c_str(), NULL); /* Execute the program */

发生的事情是java将“-jar”作为路径,因此我的命令实际上是

java pathToJar input

而不是

java -jar pathToJar input

修改:更正后的代码段:

bool decrypt_file(const std::string& javaPath, const std::string& file) {
        int result, status;
        int pipefd[2];
        FILE *cmd_output;
        char buf[1024];
        int ex;
        const std::string decryptJar = workingDir() + "/util/decrypt.jar";

        result = pipe(pipefd);
        if (result < 0) {
            throw "pipe error!";
        }

        pid_t pid = fork(); /* Create a child process */
        if ( !fileExists(decryptJar) ) throw "File decryptor does not exist!";

        switch (pid) {
            case -1: /* Error */
    #ifdef _DEBUG
                std::cout<<"fork() failed!\n";
    #endif
                return false;
            case 0: /* Child process */
                dup2(pipefd[1], STDOUT_FILENO); /* Duplicate writing end to stdout */
                close(pipefd[0]);
                close(pipefd[1]);

                ex = execl(javaPath.c_str(), decryptJar.c_str(), "-jar", decryptJar.c_str(), file.c_str(), NULL); /* Execute the program */     
    #ifdef _DEBUG
                std::cout << "execl() failed! returned "<<ex<<", errno = "<<errno<<"\n"; /* execl doesn't return unless there's an error */
    #endif
                if ( errno == 2 ) {
                    std::cout<<"JAVA NOT FOUND! Check if java is installed and/or if the path in the config file points to a correct java installation!\n\n";
                }
                return false;
            default: /* Parent process */
                close(pipefd[1]); /* Close writing end of pipe */
                cmd_output = fdopen(pipefd[0], "r");
    #ifdef _DEBUG
                if (fgets(buf, sizeof buf, cmd_output)) {
                    std::string str(buf);
                    if ( str != "OK" )
                        std::cout<<"---DECRYPT OUTPUT: "<<str<<"\n";
                }
    #endif
                while (!WIFEXITED(status)) {
                    waitpid(pid, &status, 0); /* Wait for the process to complete */
                }

    #ifdef _DEBUG
                std::cout << "Decryptor exited with " << WEXITSTATUS(status) << "\n";
    #endif
                return true;
        }
    }