简体   繁体   English

如何使用C ++打开Java程序

[英]How do I open a Java program using C++

I am trying to make a Java program, but I want it to be opened by C++. 我正在尝试制作Java程序,但我希望它可以由C ++打开。

I have been this using in C++ 我一直在用C ++

system("java -jar MMQ4.jar");

But it requires the users to add C:\\Program Files\\Java\\jre7\\bin\\ to their PATH environmental variable 但这要求用户将C:\\ Program Files \\ Java \\ jre7 \\ bin \\添加到其PATH环境变量中

So, is their another way to run a Java program using C++? 那么,他们是使用C ++运行Java程序的另一种方式吗?

running java files does NOT require the user to add something to environment variables, thats simply wrong as the Java-installer already does that automatically. 运行Java文件不需要用户向环境变量中添加内容,这完全是错误的,因为Java安装程序已经自动执行此操作。

If you cant run the the java-binary without adding something to the environment your JRE is faulty and you need to reinstall. 如果您无法在不向环境中添加任何内容的情况下运行Java二进制文件,则您的JRE有问题,您需要重新安装。 Please dont believe everything you read on internet forums, this statement is a really old myth, it was true about 5 years ago for JDK-releases but everyting after that .... not so much 请不要相信您在互联网论坛上阅读的所有内容,这是一个非常古老的神话,大约在5年前,对于JDK版本来说是正确的,但此后的一切....

One solution, not nice in my opinion is: 我认为不好的一种解决方案是:

system("export LD_LIBRARY_PATH=\"<java_path>\" && java -jar MMQ4.jar");

This solution is valid for unix so 该解决方案对UNIX有效,因此

For windows, take a look here . 对于Windows,请在这里看看。

update: 更新:

By the way, if I understand what are your intentions, you should use: 顺便说一句,如果我了解您的意图,则应使用:

fork
exec
wait

rather than system. 而不是系统。

const int pid = fork();
execv("java", "-jar MMQ4.jar"");

In this way you have a reference to PID's process and kill it when you want. 这样,您就可以引用PID的过程,并在需要时将其终止。

Have you tried using the -classpath option to the Java interpreter. 您是否尝试过对Java解释器使用-classpath选项。 It's been some time since I've used Java, but in the past, that would have worked. 自从我使用Java已经有一段时间了,但是在过去,那是行得通的。

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

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