简体   繁体   English

在Java中执行批处理文件会产生意外结果

[英]Executing Batch file in java gives unexpected results

I have the following batch File : - 我有以下批处理文件:-

gcc temp.c -o temp.exe
pause

This batch file compiles a .c file. 该批处理文件将编译一个.c文件。 This batch script runs fine when executed by double clicking, but When I use java for that purpose, It gives me an error. 通过双击执行时,此批处理脚本运行良好,但是当我为此目的使用Java时,它给我一个错误。 The following is the code 以下是代码

Process p = Runtime.getRuntime().exec("cmd /c start CPrun.bat");
p.waitFor();

CRun.bat is the name of the batch file. CRun.bat是批处理文件的名称。

The following is the error on cmd 以下是cmd上的错误

'gcc' is not recognized as an internal or external command, 
operable program or batch file.

Note that gcc is my Compiler for C which is defined in the path of my Computer 请注意,gcc是我的C编译器,它在计算机的路径中定义

Specifying the full path should do the trick. 指定完整路径应该可以解决问题。 But don't forget to surround it with double quotes " . In batch it is recommended to surround all your paths with double quotes because in windows paths are very likely to contain whitespaces. Without the double quotes the whitespace will be handled as a delimiter, braking your paths in distinct parts (as if they were different arguments/executables). 但是不要忘了用双引号将其引起来。 "批处理时,建议所有路径都用双引号引起来,因为在Windows中,路径很可能包含空格。如果没有双引号,则空格将作为分隔符处理,在不同的部分刹车(就像它们是不同的参数/可执行文件一样)。

About the environment variables: you will only have access to the environment variables your system passed to your JVM. 关于环境变量:您将只能访问将系统传递给JVM的环境变量。 To see these variables you can use System.getenv() it returns a string to string map, mapping environment variable names on their value. 要查看这些变量,可以使用System.getenv()将字符串返回到字符串映射,并在其值上映射环境变量名称。 I have made a little demo where all available environment variables are printed. 我做了一个小样 ,其中打印了所有可用的环境变量。

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

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