简体   繁体   中英

How to launch a jar file with c++ exe?

I have a file .jar and i want to launch it with a c++ exe.

I have written inside the file exe this code:

#include <windows.h>
#include <iostream>

using namespace std;

int main()
{

   system("cd E:\\Test\\Test 1.0");
   system("java -jar Test.jar");

}

But it gives me this output: "Unable to access jarfile Test.jar"

How could i solve this problem?

Each call to "system" is independant to the former one, so your second call to system has not the working directory you think it has, but a default one. You either need to do everything regarding the execution of the Jar in one system call or, much better and easier, simply provide the absolute path to the Jar in your system call.

Invokes the command processor to execute a command.

system

There's no sentence about that the command processor is persistant for different calls to system or that one and the same us used, so state is preserved etc. That's simypl not the case, one call most of the times directly refers to one execution of cmd.exe on Windows or any default shell on Linux or whatever.

This will not work since the full path can't have spaces you will have to specify the full path like this "E:\\\\Test\\Test 1.0\\Test.jar" including the double quotes " "

  int main()
  {

  system("java -jar \"E:\\\\Test\\Test 1.0\\Test.jar\"");

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM