g++: File not found

2019-02-26 23:21发布

I have been developing a program lately that compiles and runs a C++ Program from a Java program, I have gotten everything working basically (or atleast to my knowledge) but then I noticed some things being printed to the Error Stream:

cdog5000@srv3:~$ 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


cdog5000@srv3:~$ 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
cdog5000@srv3:~$ sudo g++ --static -o "/home/cdog5000/cody.out" "/home/cdog5000/cody.cpp"

As you can see it works if I do it via the SSH but not the Java code?

The Java code:

 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;
    }

Thanks for any help and it is apprectiated!

标签: java linux g++
2条回答
虎瘦雄心在
2楼-- · 2019-02-26 23:31

GCC doesn't lie like that - it looks like the file isn't there. Are you sure that you're showing us the output from the correct directories?

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-26 23:33
Err: g++: "/home/cdog5000/cody.cpp": No such file or directory

Is telling you the problem.

You have one level of quotes too many, so you're looking for "/home/cdog5000/cody.cpp" rather than /home/cdog5000/cody.cpp.

The Runtime.exec documentation says:

More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.

Meaning it only splits on whitespace, it doesn't handle double quotes like the shell does.

Many languages have two functions, one called exec which runs the command verbatim, and system which passes the string to the shell, where it will split words and expand wildcards.

I can't see a system call in Java, so I think you will have to use exec(String[] cmdarray) rather than exec(String command).

查看更多
登录 后发表回答