我当时正在研究用于编译程序的Java代码。
因此,用户可以选择要编译的文件,然后该程序将通过自己的g ++运行。
在网上,特别是在StackOverFlow上,我决定使用以下代码:
//Set a FileChoose called fc, got the file path (filePath) and the directory path (dirPath), then:
ProcessBuilder process=null;
try {
process = new ProcessBuilder("g++", filePath, " -o "+dirPath+"/a.out").start();
} catch (IOException ex) {
System.err.println("Error compiling file");
Logger.getLogger(Nuovo.class.getName()).log(Level.SEVERE, null, ex);
System.exit(0);
}
关键是它不会返回任何错误,并且当我检查文件是否已编译时,什么也没有。
任何想法?
非常感谢你!
我建议您阅读程序的输出以查看程序产生了什么错误。
我的猜测是它会说类似的话。
File not found: -o dir/a.out
请注意,您已指定了" -o "+dirPath+"/a.out"
是单个参数。这就像写作
g++ $filePath ' -o dir/a.out'
也许你的意图是
new ProcessBuilder("g++", filePath, "-o", dirPath+"/a.out").start();
Java为什么不像shell一样解析您的参数?因为它将参数传递给系统调用,exec
所以它实际上不会执行C ++不会执行的任何操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句