java - Unable to launch jar with execl() -


i given decrypt.jar , encrypt.jar file, used prepare files before transmitting.

when launch terminal , type:

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

i output:

no input file specified 

which ok! jar works. in code, when launch jar execl(), output:

error: not find or load main class util.decrypt.jar decryptor exited 0 

notice issue here java tried launch class path jar (the path util/decrypt.jar, , executed class util.decrypt.jar)

my code:

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 child process */     const std::string decryptjar = "util/decrypt.jar";     int ex;     if ( !fileexists(decryptjar) ) throw "file decryptor 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 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 program */ #ifdef _debug             std::cout << "execl() failed! returned "<<ex<<", errno = "<<errno<<"\n"; /* execl doesn't return unless there's error */             //todo if errno 2, java not found on system, let 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 process complete */             }  #ifdef _debug             std::cout << "decryptor exited " << wexitstatus(status) << "\n"; #endif             return true;     } } 

the manifest inside jar correct (it generated eclipse):

manifest-version: 1.0 class-path: . main-class: com.{...}.decryptor 

add:

trying change path absolute path of jar didn't fix problem.

const std::string decryptjar = workingdir() + "/util/decrypt.jar"; 

solved

stupid mistake side, first argument should, convention, path executable.

so

ex = execl(config::getinstance().getjava().c_str(), "-jar", decryptjar.c_str(), file.c_str(), null); /* execute program */ 

should be

ex = execl(config::getinstance().getjava().c_str(), decryptjar.c_str(), "-jar", decryptjar.c_str(), file.c_str(), null); /* execute program */ 

what happened java took "-jar" path, command actually

java pathtojar input 

instead of

java -jar pathtojar input 

edit: corrected code snippet:

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 child process */         if ( !fileexists(decryptjar) ) throw "file decryptor 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 stdout */                 close(pipefd[0]);                 close(pipefd[1]);                  ex = execl(javapath.c_str(), decryptjar.c_str(), "-jar", decryptjar.c_str(), file.c_str(), null); /* execute program */          #ifdef _debug                 std::cout << "execl() failed! returned "<<ex<<", errno = "<<errno<<"\n"; /* execl doesn't return unless there's error */     #endif                 if ( errno == 2 ) {                     std::cout<<"java not found! check if java installed and/or if path in config file points 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 process complete */                 }      #ifdef _debug                 std::cout << "decryptor exited " << wexitstatus(status) << "\n";     #endif                 return true;         }     } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -