1

I use chdir() to switch the directory, and then use execvp() to execute "java Main". I'm sure there is Main.class, but something went wrong. I want to know why.

#include <cstdio>
#include <unistd.h>
using namespace std;
int main(){
    char buf[80];
    getcwd(buf,sizeof(buf));
    printf("current working directory: %s\n", buf);
    chdir("/home/keane/Judge/temp");
    getcwd(buf,sizeof(buf));
    printf("current working directory: %s\n", buf);
    char *array[3];
    array[0] = "java";
    array[1] = "Main";
    array[2] = NULL;
    execvp("java", array);
    return 0;
}

the error is could not find the main class , and I can run java Main in that directory.

What drives me crazy is that I can't use system("java Main"), and the error is that Error: Could not find or load main class Main, and it's just like this on my computer

update:

#include <unistd.h>
#include <cstdlib>
int main(){
    chdir("/home/keane/Judge/temp");
    system("pwd");
    system("ls");
    system("java Main");
    return 0;
}

the output on console is:

/home/keane/Judge/temp
1.out  3.out  5.out   Main.class  stdout_spj.txt
2.out  4.out  ce.txt  Main.java
Error: Could not find or load the main class Main

my final solution is to reboot the computer and add -cp . to the java command. althought I don't why is necessary. thanks everyone!

zxCoder
  • 71
  • 1
  • 5

2 Answers2

2

This works as intended on my system, maybe you need to add -cp . to your java call.

EDIT: to elaborate: -cp (for classpath) tells java where to look for user provided .class files. This does not necessarily include the current working directory by default.

Peter
  • 2,919
  • 1
  • 16
  • 35
  • The problem is on the code, maybe sometimes it can work but it shouldn't because how the function `execvp` works. – cdecompilador Sep 12 '20 at 11:24
  • @cdecompilador: Your answer makes no sense at all, why would the output not be visible just because the current process is replaced by `java`? – Peter Sep 12 '20 at 12:18
  • Thats how the function `execvp` works as far as I undestanded, it shadows the process but its non-blocking – cdecompilador Sep 12 '20 at 12:30
  • @cdecompilador: That's not true, it completely _replaces_ the current process. Any code after the _execvp_ call will never be executed if _execvp_ succeeds. See e.g. https://man7.org/linux/man-pages/man3/exec.3.html – Peter Sep 12 '20 at 12:31
0

The execution of execvp() is non-blocking and takes ownership of the caller, that means that when it starts if the program ends too quickly you will never be able to see the result, to solve this I use fork(). The wait is just to avoid using sleep as I used at the begining. Its all in c.

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char** argv){
    char buf[80];
    getcwd(buf,sizeof(buf));
    printf("current working directory: %s\n", buf);
    chdir("/home/");
    getcwd(buf,sizeof(buf));
    printf("current working directory: %s\n", buf);
    char *array[3] = {"java", "Main", NULL};
    if(fork() == 0) {
        if(execvp("java", array) < 0) {
            fprintf(stderr, "Error spawning command: %s\n", strerror(errno));
        }
    } else {
        printf("Command spawned\n");
        wait(NULL); // Wait to the forked process to end (avoid using sleep)
    }
    return 0;
}
  • I'm very confused. I copied your code completely, only modified the directory of the Main.class. But the error is still occur. I don't know if it's my java environment problem, but I can run "java Main" in this path. – zxCoder Sep 12 '20 at 11:25
  • It shows you some kind of error? If the command fails it should show something similar to `Error spawning command: `. This works in my system – cdecompilador Sep 12 '20 at 11:29
  • no, just show "error: Could not found the main class." – zxCoder Sep 12 '20 at 11:34
  • I changed the chdir() function because to make it work on my system – cdecompilador Sep 12 '20 at 11:35
  • I know, I change the parameter to the path of my Main.class – zxCoder Sep 12 '20 at 11:37
  • Lets make a little test first if instead of the `java` command you put `g++ -v` in the `execvp()` function to ensure it works correctly, because it seems to be a problem on the path – cdecompilador Sep 12 '20 at 11:40
  • I tried some other commands in the execvp (), maybe I can say that everything is ok except `java` – zxCoder Sep 12 '20 at 11:47
  • A couple comments ago you said that the error was `error: Could not found the main class`. Maybe in the path you put in chdir() threre is no Main class or try appending the `.class` to the command invocation. Its an weird error. – cdecompilador Sep 12 '20 at 11:51
  • More bizarrely, I can execute `javac Main.java` in execvp(), but the `java` can't – zxCoder Sep 12 '20 at 11:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221370/discussion-between-cdecompilador-and-zxcoder). – cdecompilador Sep 12 '20 at 12:12