使用Java Runtime从任何位置执行cl命令。

huangapple go评论80阅读模式
英文:

Execute cl command with Java Runtime from anywhere

问题

我正在尝试使用Java Runtime调用/执行C++编译器命令。

Runtime runtime = Runtime.getRuntime();
String[] command = { "cl", "/P", "/EP", "/C", "-D", "LOC" + loc_file, file};
Process process = runtime.exec(command);
process.waitFor();

然而,我总是得到以下错误消息:

"Cannot run program "cl.exe" CreateProcess error=2, The system cannot find the file specified."

显然它找不到cl程序。然而,我不太知道如何配置Java Runtime,以便找到cl程序。是否有一种方法可以设置某种类型的PATH变量,以便找到cl程序?

如果我想在cmd中调用cl程序,我首先必须运行位于"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build"中的"vcvars32.bat"。然后我可以随心所欲地使用cl程序。然而,我不知道如何通过Java代码来实现这一点。

英文:

I am trying to call/execute a C++ compiler command with the Java Runtime.

Runtime runtime = Runtime.getRuntime();
String[] command = { "cl", "/P", "/EP", "/C", "-D", "LOC" + loc_file, file};
Process process = runtime.exec(command);
process.waitFor();

However I always get following error message:

"Cannot run program "cl.exe" CreateProcess error=2, The system cannot find the file specified."

So obviously it doesn't find the cl program. However, I don't really know how to configure the Java Runtime, in order to find the cl program. Is there a way to set some sort of PATH variable for it to find the cl program?

If I want to call the cl program in cmd, I first have to run vcvars32.bat which is located in C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build. Then I can use the cl program as I wish. However I have no clue, how I can accomplish this through Java Code.

答案1

得分: 0

在你的情况下,我认为有两种解决方案可以解决你的问题:

1 在你的系统上更改路径,并在这个更新的环境中启动程序

根据你在问题下的评论,这是你已经做过的事情。

你可以通过调整环境变量手动将你想要启动的程序添加到系统的“PATH”中,然后使用更新的环境重新启动你的Java程序。

2 在Eclipse中,仅修改你的程序的PATH环境变量

由于你提到你在使用Eclipse,有一个替代方法可以修改操作系统配置中的PATH。

在Eclipse的“运行配置”中,有一个“环境”选项卡,你可以在其中为你的特定程序定义新的环境变量或修改现有的环境变量。你可以在这里修改PATH,类似于这样:

使用Java Runtime从任何位置执行cl命令。

英文:

In your situation, I think there are two solutions to your issue:

1 Change the PATH on your system and launch the program in this updated environment

This what you did already judging by your comment under the question.

You can manually add the program you want to launch in the "PATH" of your system by adjusting the environment variables and then launching your java program anew with the updated environment.

2 Within Eclipse, modify the PATH environment variable for your program only

Since you mentioned you are using Eclipse, there is an alternative to modifying the PATH in the operating system configuration.

In the Run Configurations of Eclipse, there is an Environment tab in which you can define new environment variables or modify existing ones for your specific program. You can make the modification to the PATH here with something like this:

使用Java Runtime从任何位置执行cl命令。

huangapple
  • 本文由 发表于 2020年8月4日 12:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63240411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定