gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:10:21 24 4
gpt4 key购买 nike

我使用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,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;
}
错误是 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命令。
虽然我不明白为什么有必要。
感谢大家!

最佳答案

这在我的系统上按预期工作,也许您需要将-cp .添加到您的java调用中。
编辑:详细说明:-cp(用于类路径)告诉java在哪里寻找用户提供的.class文件。默认情况下,这不一定包括当前工作目录。

关于c++ - 我调用execvp运行Java时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63859556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com