我一直在開發一個程序,最近編譯並運行一個Java程序的C++程序,我得到的一切工作基本上(或至少我的知識),但後來我注意到一些東西被打印到錯誤流:g ++:找不到文件
[email protected]:~$ java -Xmx50m -jar main2.jar
Running Command: sudo g++ --static -o "/home/cdog5000/cody.out" "/home/cdog5000/cody.cpp"
Err: g++: "/home/cdog5000/cody.cpp": No such file or directory
Err: g++: no input files
[email protected]:~$ ls -l
total 4548
-rwxr-xr-x 1 cdog5000 cdog5000 1297588 Feb 3 23:11 a.out
-rwxr-xr-x 1 cdog5000 cdog5000 7978 Feb 2 04:39 cody
-rw-r--r-- 1 cdog5000 cdog5000 106 Feb 4 02:09 cody.cpp
-rwxr-xr-x 1 cdog5000 cdog5000 1297357 Feb 4 02:09 cody.out
-rw-r--r-- 1 root root 410433 Feb 4 02:48 log.txt
-rwxr-xr-x 1 cdog5000 cdog5000 801088 Feb 1 05:24 main.jar
-rw-r--r-- 1 cdog5000 cdog5000 804802 Feb 4 02:49 main2.jar
drwxr-xr-x 3 cdog5000 cdog5000 4096 Feb 3 23:11 sandbox
[email protected]:~$ sudo g++ --static -o "/home/cdog5000/cody.out" "/home/cdog5000/cody.cpp"
正如你可以看到它的工作原理,如果我通過SSH而不是Java代碼?
的Java代碼:
public static Exec exec(String cmd){
Exec exec = new Exec(cmd);
try {
long current = System.currentTimeMillis();
Process proc = Runtime.getRuntime().exec(cmd);
exec.setReturnValue(proc.waitFor());
exec.setRunTime(System.currentTimeMillis() - current);
BufferedInputStream bos = new BufferedInputStream(proc.getInputStream());
byte b[] = new byte[1024];
String content = "";
while(bos.read(b) != -1) {
content += new String(b);
}
exec.setStdIn(content.split("\n"));
content = "";
bos = new BufferedInputStream(proc.getErrorStream());
while(bos.read(b) != -1) {
content += new String(b);
}
exec.setStdErr(content.split("\n"));
} catch (Exception e) {
e.printStackTrace();
}
return exec;
}
感謝您的幫助,它是apprectiated!
也許g ++正在尋找一個正好名爲「/home/cdog5000/cody.out」的文件 - 即兩端都有引號。也許嘗試刪除引號...? – CanSpice 2011-02-04 00:11:47
令人驚歎的,修復它。發佈它作爲答覆,我會給你接受答案。 – cdog5000 2011-02-04 00:15:31
哦,我錯過了一個被接受的答案。好吧。這就是生活。 – CanSpice 2011-02-04 00:25:14