简体   繁体   English

如何使用C ++ exe启动jar文件?

[英]How to launch a jar file with c++ exe?

I have a file .jar and i want to launch it with a c++ exe. 我有一个.jar文件,我想用C ++ exe启动它。

I have written inside the file exe this code: 我已经在文件exe中编写了以下代码:

#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" 但这给了我以下输出:“无法访问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. 您要么需要在一个系统调用中完成与执行Jar有关的所有事情,要么变得更好,更轻松,只需在系统调用中提供指向Jar的绝对路径即可。

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. 没有关于命令处理器对于不同的系统调用是持久的,还是我们使用的相同,因此保留了状态的说法,这是不正确的。情况并非如此,大多数情况下,一次调用直接指的是cmd的一次执行。 Windows上的exe或Linux上的任何默认Shell或任何其他版本。

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 " " 由于完整路径不能包含空格,因此这将不起作用,您将必须指定完整路径,例如“ E:\\\\ Test \\ Test 1.0 \\ Test.jar”,其中包括双引号“”

  int main()
  {

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

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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