我调用execvp运行Java时发生错误

问题描述

我使用chdir()切换目录,然后使用execvp()执行“ java Main”。我确定有Main.class,但是出了点问题。我想知道为什么。

#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,buf);
    char *array[3];
    array[0] = "java";
    array[1] = "Main";
    array[2] = NULL;
    execvp("java",array);
    return 0;
}

错误是could not find the main class,我可以在该目录中运行java Main

让我发疯的是我不能使用system("java Main"),而错误是Error: Could not find or load main class Main,就像我的计算机上这样

更新:

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

控制台上的输出为:

/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

我的最终解决方案是重新启动计算机,并将-cp .添加到java命令。 尽管我不明白为什么有必要。 谢谢大家!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)